Php Array_Search issue - php

I've been trying to work with the array_search function.
This is my array :
array (size=23)
0 =>
array (size=3)
0 => string '15' (length=2)
1 => float 115.08386533184
2 => string '2014-02-06 21:00:00' (length=19)
1 =>
array (size=3)
0 => string '81' (length=2)
1 => float 100.41587590619
2 => string '2014-03-28 00:00:00' (length=19)
2 =>
array (size=3)
0 => string '65' (length=2)
1 => float 99.096448338334
2 => string '2014-02-08 21:00:00' (length=19)
3 =>
array (size=3)
0 => string '53' (length=2)
1 => float 98.752479251378
2 => string '2014-03-05 12:00:00' (length=19)
4 =>
array (size=3)
0 => string '24' (length=2)
1 => float 98.303557178126
2 => string '2014-02-07 21:00:00' (length=19)
5 =>
array (size=3)
0 => string '23' (length=2)
1 => float 98.270536817788
2 => string '2014-02-08 21:30:00' (length=19)
6 =>
array (size=3)
0 => string '37' (length=2)
1 => float 98.139812350661
2 => string '2014-02-06 23:00:00' (length=19)
7 =>
array (size=3)
0 => string '13' (length=2)
1 => float 97.810954038756
2 => string '2014-02-09 19:45:00' (length=19)
8 =>
array (size=3)
0 => string '22' (length=2)
1 => float 95.975221202728
2 => string '2014-02-11 20:30:00' (length=19)
9 =>
array (size=3)
0 => string '66' (length=2)
1 => float 94.115075316114
2 => string '2014-02-04 19:00:00' (length=19)
10 =>
array (size=3)
0 => string '52' (length=2)
1 => float 93.957291067159
2 => string '2014-03-15 00:00:00' (length=19)
11 =>
array (size=3)
0 => string '12' (length=2)
1 => float 89.188128768086
2 => string '2014-05-17 10:00:00' (length=19)
12 =>
array (size=3)
0 => string '51' (length=2)
1 => float 88.381986169995
2 => string '2014-03-27 12:00:00' (length=19)
13 =>
array (size=3)
0 => string '98' (length=2)
1 => float 85.240471279545
2 => string '2014-02-17 18:00:00' (length=19)
14 =>
array (size=3)
0 => string '92' (length=2)
1 => float 82.721601210972
2 => string '2014-02-06 20:15:00' (length=19)
15 =>
array (size=3)
0 => string '82' (length=2)
1 => float 82.473535719129
2 => string '2014-02-27 17:00:00' (length=19)
16 =>
array (size=3)
0 => string '121' (length=3)
1 => float 56.833974620724
2 => string '2014-02-07 22:30:00' (length=19)
17 =>
array (size=3)
0 => string '120' (length=3)
1 => float 36.269423317467
2 => string '2014-02-06 21:30:00' (length=19)
18 =>
array (size=3)
0 => string '83' (length=2)
1 => float 35.863780709688
2 => string '2014-02-21 07:00:00' (length=19)
19 =>
array (size=3)
0 => string '35' (length=2)
1 => float 24.322967350005
2 => string '2014-02-07 21:30:00' (length=19)
20 =>
array (size=3)
0 => string '6' (length=1)
1 => float 19.71405484708
2 => string '2014-02-14 22:00:00' (length=19)
21 =>
array (size=3)
0 => string '45' (length=2)
1 => float 19.142717452481
2 => string '2014-03-15 00:00:00' (length=19)
22 =>
array (size=3)
0 => string '117' (length=3)
1 => float 17.641278106673
2 => string '2014-02-18 17:30:00' (length=19)
Now - The thing is I'm using array_search in order to get the key of a specific ID (Note : The ID is the first value in each array). The thing is, it doesn't work, probably because it can't get the key of the array itself.
Let me show you an example : ($array = The array above)
$r = array_search(53, array_keys($array));
I want $r to be equal to 3, because thats they array's key position.
Any idea how I can do it?

$r = array_search('53', array_map(function($x) { return $x[0]; }, $array));
array_map will construct an array of element 0 of each element of the array. Then you search for 53 in this, and $r will be its position.
Or you could simply write a loop:
foreach ($array as $i => $el) {
if ($el[0] == '53') {
$r = $i;
$result = $el;
break;
}
}

I searched SO and found a similar topic: PHP Multidimensional Array - Search for value and get the sub-array.
I slightly modified one of the solutions provided above.
$id=53;
$r=null;
$data=null;
foreach ($array as $idx=>$subArray) {
if ( $subArray[0] == $id ) {
$r=$idx;
$target=$subArray;
break;
}
}

use array_map function and array_filter function.
function find($a, $id) {
return ($a[0] == $id) ? $a : false;
}
function filter($a) {
return $a;
}
$r = array_filter(array_map("find", $array, array(53)), "filter");

A better call than array_map() to isolate the first column of values is: array_column(). This is the task that the function was created for.
Use this implementation:
$r = array_search('53', array_column($array,0));
Done.

Related

Adding values to the end of each index of an array in php

I have two arrays in PHP with a number of equal indices like this:
ARRAY 1 :
array (size=3)
0 =>
array (size=8)
0 => int 1
1 => int 17
2 => int 145
3 => string 'one' (length=3)
4 => string '#3546b140' (length=9)
5 => string '5' (length=1)
6 => string 'ONE' (length=3)
7 => string 'ONE' (length=3)
1 =>
array (size=8)
0 => int 2
1 => int 30
2 => int 224
3 => string 'two' (length=3)
4 => string '#3546b140' (length=9)
5 => string '6' (length=1)
6 => string 'TWO' (length=3)
7 => string 'TWO' (length=3)
2 =>
array (size=8)
0 => int 3
1 => int 31
2 => int 120
3 => string 'thr' (length=3)
4 => string '#3546b140' (length=9)
5 => string '7' (length=1)
6 => string 'THR' (length=3)
7 => string 'THR' (length=3)
This is the second array
array (size=3)
1 => int 1761693
2 => int 8911775
3 => int 3510858
This would be the desired result, look at the eighth position of each index, any ideas?
array (size=3)
0 =>
array (size=8)
0 => int 1
1 => int 17
2 => int 145
3 => string 'one' (length=3)
4 => string '#3546b140' (length=9)
5 => string '5' (length=1)
6 => string 'ONE' (length=3)
7 => string 'ONE' (length=3)
8 => int 1761693
1 =>
array (size=8)
0 => int 2
1 => int 30
2 => int 224
3 => string 'two' (length=3)
4 => string '#3546b140' (length=9)
5 => string '6' (length=1)
6 => string 'TWO' (length=3)
7 => string 'TWO' (length=3)
8 => int 8911775
2 =>
array (size=8)
0 => int 3
1 => int 31
2 => int 120
3 => string 'thr' (length=3)
4 => string '#3546b140' (length=9)
5 => string '7' (length=1)
6 => string 'THR' (length=3)
7 => string 'THR' (length=3)
8 => int 3510858
I have tried to do this but it does not work!!!
array_merge($array1,$array2);
and this
array_push($array1, $array2);
Iterating with a foreach but I can't position correctly the arrays by indices, any idea?
Loop through the first array and add the value from the second:
foreach ($array1 as $index => $subArray) {
// We use $index + 1 since the first starts with 0 and the second with 1
// We're also using $array1 since $subArray is just a copy so adding to that
// won't change the original array
$array1[$index][] = $array2[$index + 1];
}

Change the key in a PHP array

I want to change the key of my array in php.
Here an exemple :
array (size=5)
0 =>
array (size=2)
'iden' => string '01' (length=8)
'don' => string '17' (length=2)
1 =>
array (size=2)
'iden' => string '02' (length=8)
'don' => string '17' (length=2)
2 =>
array (size=2)
'iden' => string '03' (length=8)
'don' => string '17' (length=2)
And I want to change my array like this :
array (size=5)
0 =>
array (size=2)
0 => string '01' (length=8)
1 => string '17' (length=2)
1 =>
array (size=2)
0 => string '02' (length=8)
1 => string '17' (length=2)
2 =>
array (size=2)
0 => string '03' (length=8)
1 => string '17' (length=2)
Thanks in advance
You can use the array_values function to remove named keys:
foreach($array as &$item) {
$item = array_values($item);
}
unset($item); // Remove reference
Note the & in the foreach. This creates a reference in the $item variable to the corresponding array element which means you can edit it in your loop.
If you want, you can also write this in a single line using array_map:
$array = array_map("array_values", $array);

PHP Array for hard multilevel items

i have this array structure and i want to create other array structure with that:
array (size=7)
0 =>
array (size=9)
0 => string 'Dorado' (length=6)
1 => string '64GB' (length=4)
2 => string 'Plastico' (length=8)
1 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '32GB' (length=4)
2 => string 'Plastico' (length=8)
2 =>
array (size=9)
0 => string 'Blanco' (length=6)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
3 =>
array (size=9)
0 => string 'Verde' (length=5)
1 => string '64GB' (length=4)
2 => string 'Madera' (length=6)
4 =>
array (size=9)
0 => string 'Azul' (length=4)
1 => string '128GB' (length=5)
2 => string 'Cristal' (length=7)
5 =>
array (size=9)
0 => string 'Azul' (length=4)
1 => string '128GB' (length=5)
2 => string 'Plastico' (length=8)
6 =>
array (size=9)
0 => string 'Azul' (length=4)
1 => string '64GB' (length=4)
2 => string 'Cristal' (length=7)
Each array item have a group of characteristics like:
0 => string 'Dorado' (length=6)
1 => string '64GB' (length=4)
2 => string 'Plastico' (length=8)
....
0 => string 'Azul' (length=4)
1 => string '128GB' (length=5)
2 => string 'Cristal' (length=7)
I need an array that have this values like
Azul=>64GB=>Plastico
128GB=>Cristal
if sub item repeats only write the children value
Azul=>64GB=>Cristal Only adds Cristal because 64GB exists
I have this code in php:
foreach ($arrCaracteristicas as $itemMadre => $valueMadre) {
$NombreNodo = $valueMadre[0];
if ($NumeroCaracteristicas > 1) {
for ($i = 1; $i <= $NumeroCaracteristicas - 1; $i++) {
$NombreCaract = $valueMadre[$i];
echo "$NombreNodo: $NombreCaract <br>";
array_push( $arrSelectMain[$NombreNodo], $NombreCaract);
if (!isset($arrSelectMain[$NombreNodo][$NombreCaract])) {
//$arrSelectMain[$NombreNodo]["d"] = $NombreCaract;
}
}
}
}
Where $arrCaracteristicas have the long array, and $arrSelectMain have the first level of the array keys:
Code of first level:
foreach ($arrCaracteristicas as $item => $value) {
$arrSelectMain[$value[0]] = $value[0];
}
asort($arrSelectMain);
$arrSelectMain = array_unique($arrSelectMain);
This is an image with structure that i want:
https://ibb.co/Q8k198Y
If somebody can help me i will invite a beer in paypal :)
This should work, first separate the array into columns and then loop over the items.
$first = array_column($items, 0);
$second = array_column($items, 1);
$third = array_column($items, 2);
$output = [];
for ($i = 0; $i < count($first); $i++) {
$output[$first[$i]][$second[$i]][] = $third[$i];
}
Have a look at the sample

How to improve an algorithm where we merge lines of an array

I ‘ve got an array like this =>
this is a result from a google analytics request. I asked for the amout of visits for the last three months.
$statPerMonth
array (size=2)
'08' => // The month (August)
array (size=34)
0 =>
array (size=3)
0 => string '08' (length=2) // Month again
1 => string 'admin.testweb.fr' (length=19) // host
2 => string '1' (length=1) // amount of visits
1 =>
array (size=3)
0 => string '08' (length=2)
1 => string 'audigie-espace-auto.reseau-fivestar.fr' (length=38)
2 => string '6' (length=1)
2 =>
array (size=3)
0 => string '08' (length=2)
1 => string 'www.audigie-espace-auto.reseau-fivestar.fr' (length=31)
2 => string '9' (length=1)
3 =>
array (size=3)
0 => string '08' (length=2)
1 => string 'carrosserie-abberis.reseau-fivestar.fr' (length=38)
2 => string '7' (length=1)
'07' =>
array (size=47)
0 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'www.anothersite.testweb.fr' (length=13)
2 => string '1' (length=1)
1 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'admin.testweb.fr' (length=16)
2 => string '2' (length=1)
2 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'admin.testweb.fr' (length=19)
2 => string '1' (length=1)
3 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'audigie-espace-auto.reseau-fivestar.fr' (length=38)
2 => string '20' (length=2)
4 =>
array (size=3)
0 => string '07' (length=2)
1 => string 'www.admin.testweb.fr' (length=19)
2 => string '1' (length=1)
This array respresent the amount of visits for my websites but
you can see that the values [‘08’][‘1’] and [‘08’][‘2’] are identicals (only ‘www.’ differs)
I want to merge those cells and add their value (because it’s the same site !) in order to get the total amount of visits for a site with it’s two hostnames.
Consider $sites as an array of Site Object (websites).
the getHost() method will return the site host for exemple ‘my-host.fr’ without the ‘www’
consider $statsPerMonth array explained above
finally consider this algorithm
foreach ($statsPerMonth as $actualMonth => $stats) {
foreach($sites as $site) {
$siteHost = $site->getHost();
foreach ($stats as $row) {
if (strstr($row['1'], $siteHost)) {
if(isset($globalStats[$actualMonth][$siteHost])) {
$globalStats[$actualMonth][$siteHost] = $globalStats[$actualMonth][$siteHost] + $row['2'];
} else {
$globalStats[$actualMonth][$siteHost] = 0;
$globalStats[$actualMonth][$siteHost] = $globalStats[$actualMonth][$siteHost] + $row['2'];
}
}
if(!isset($globalStats[$actualMonth][$siteHost])) {
$globalStats[$actualMonth][$siteHost] = 0;
}
}
}
}
This algorithm return the $globalStats array in this form
array (size=3)
'08' =>
array (size=43)
'carrosserie-la-cascade.reseau-fivestar.fr' => int 1
'audigie-espace-auto.reseau-fivestar.fr' => int 15
'carrosserie-abberis-fivestar.fr' => int 16
'carrosserie-arenales-jonathan.reseau-fivestar.fr' => int 0
'07' =>
array (size=43)
'carrosserie-la-cascade.reseau-fivestar.fr' => int 2
'audigie-espace-auto.reseau-fivestar.fr' => int 20
'carrosserie-abberis-fivestar.fr' => int 0
'carrosserie-arenales-jonathan.reseau-fivestar.fr' => int 4
'06' =>
array (size=43)
'carrosserie-la-cascade.reseau-fivestar.fr' => int 0
'audigie-espace-auto.reseau-fivestar.fr' => int 29
'carrosserie-abberis-fivestar.fr' => int 0
'carrosserie-arenales-jonathan.reseau-fivestar.fr' => int 4
This is exaclty what I want but I think we can improve this algorithm to make it more efficient (because the arrays are big). Have you any idea in order to make this algorithm better ?
Thank you.
Try to replace all "foreach" by "for", such as :
foreach ($array as $key => $value) {
}
by
$keys = array_keys($array);
$keyCount = count($keys);
for($i = 0; $i < $keyCount; $i++) {
$key = $keys[$i];
$value = $array[$key];
}

how to use array_map/array_walk function for multidimensional array

I have the following multidimensional array:
array (size=2)
10 =>
array (size=7)
1 =>
array (size=4)
0 => string '10view1' (length=7)
1 => string '10ins1' (length=6)
2 => string '10edit1' (length=7)
3 => string '10del1' (length=6)
2 =>
array (size=4)
0 => string '10view2' (length=7)
1 => string '10ins2' (length=6)
2 => string '10edit2' (length=7)
3 => string '10del2' (length=6)
3 =>
array (size=4)
0 => string '10view3' (length=7)
1 => string '10ins3' (length=6)
2 => string '10edit3' (length=7)
3 => string '10del3' (length=6)
11 =>
array (size=7)
1 =>
array (size=4)
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
2 =>
array (size=4)
0 => string '11view2' (length=7)
1 => string '11ins2' (length=6)
2 => string '11edit2' (length=7)
3 => string '11del2' (length=6)
3 =>
array (size=4)
0 => string '11view3' (length=7)
1 => string '11ins3' (length=6)
2 => string '11edit3' (length=7)
3 => string '11del3' (length=6)
I want to convert it to the array like:
1 => array(10 => array(
0 => string '10view1' (length=7)
1 => string '10ins1' (length=6)
2 => string '10edit1' (length=7)
3 => string '10del1' (length=6)),
11 => array(
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
)
),
2 => array(
10 => array(
0 => string '10view2' (length=7)
1 => string '10ins2' (length=6)
2 => string '10edit2' (length=7)
3 => string '10del2' (length=6)
),
11 => array(
0 => string '11view2' (length=7)
1 => string '11ins2' (length=6)
2 => string '11edit2' (length=7)
3 => string '11del2' (length=6)
)
),
3 => array(
10 => array(
0 => string '11view3' (length=7)
1 => string '11ins3' (length=6)
2 => string '11edit3' (length=7)
3 => string '11del3' (length=6)
),
11 => array(
0 => string '11view1' (length=7)
1 => string '11ins1' (length=6)
2 => string '11edit1' (length=7)
3 => string '11del1' (length=6)
)
)
please help me i confuse to solve this problem thanks in advance.
It's quite obvious for me how to implement this using foreach loop, but I wonder if it's possible to do this with PHP array functions like array_map or array_walk. Please use PHP 5.3 for the callback function. Thank you!
Using array map you can achieve like this
<?php
function merge_array($n, $m)
{
$data = array();
$data[10] = $n;
$data[11] = $m;
return $data;
}
$c = array_map("merge_array", $array[10], $array[11]);
print_r($c);
Using for loop it is very simple you can achieve like this
$count_10 = count($array[10]);
$count_11 = count($array[11]);
$count = ($count_10>$count_11)?$count_10:$count_11;
$data = array();
for($i=0; $i<$count; $i++)
{
if(isset($array[10][$i]))
{
$data[$i][] = $array[10][$i];
}
if(isset($array[11][$i]))
{
$data[$i][] = $array[11][$i];
}
}

Categories