Array_search()

I have

echo '<pre>';print_r($bayArray);echo '</pre>';

results in

Array
(
    [0] => Array
        (
            [name] => A
            [x_coord] => 1467
            [y_coord] => 92
            [width] => 24
            [height] => 18
        )

    [1] => Array
        (
            [name] => B
            [x_coord] => 1467
            [y_coord] => 110
            [width] => 24
            [height] => 18
        )

    [2] => Array
        (
            [name] => C
            [x_coord] => 1467
            [y_coord] => 128
            [width] => 24
            [height] => 18
        )

    [3] => Array
        (
            [name] => D
            [x_coord] => 1467
            [y_coord] => 146
            [width] => 24
            [height] => 18
        )

    [4] => Array
        (
            [name] => E
            [x_coord] => 1467
            [y_coord] => 164
            [width] => 24
            [height] => 18
        )

    [5] => Array
        (
            [name] => F
            [x_coord] => 1467
            [y_coord] => 182
            [width] => 24
            [height] => 18
        )

    [6] => Array
        (
            [name] => G
            [x_coord] => 1467
            [y_coord] => 200
            [width] => 24
            [height] => 18
        )

    [7] => Array
        (
            [name] => H
            [x_coord] => 1467
            [y_coord] => 218
            [width] => 24
            [height] => 18
        )

    [8] => Array
        (
            [name] => I
            [x_coord] => 1467
            [y_coord] => 236
            [width] => 24
            [height] => 18
        )

    [9] => Array
        (
            [name] => J
            [x_coord] => 1467
            [y_coord] => 254
            [width] => 24
            [height] => 18
        )

    [10] => Array
        (
            [name] => K
            [x_coord] => 1467
            [y_coord] => 272
            [width] => 24
            [height] => 18
        )

    [11] => Array
        (
            [name] => L
            [x_coord] => 1467
            [y_coord] => 290
            [width] => 24
            [height] => 18
        )

    [12] => Array
        (
            [name] => M
            [x_coord] => 1467
            [y_coord] => 308
            [width] => 24
            [height] => 18
        )

    [13] => Array
        (
            [name] => N
            [x_coord] => 1467
            [y_coord] => 326
            [width] => 24
            [height] => 18
        )

)

so, its a multidimensional array.
How do I loop through it so I can multiply the index by 18?
0 * 18
1 * 18
2 * 18


13 * 18
Im guessing something like

foreach($bayArray as $bay) {

	  echo array_search($bay['x_coord'],$bayArray) * 18);
	  echo "\r\n";					
}	

Or

foreach($bayArray as $key => $bay) {

	  echo ($key * 18)."\r\n";					
}
1 Like

my question would be why. I normally hate questions like that in threads, but I feel like youre taking a half-step towards a goal that would have a better path if we knew where you were walking.

$keys = array_keys($byArray);
$newKeys = array_map(static fn (int $index) => $index * 18), $keys);
$newArray = array_combine($newKeys, $byArray);

But I agree with @m_hutley , this feels like a symptom of a bigger problem.

Yes and the arrays keep changing as if just being created for the question rather than solving a real script problem…

1 Like