PHP renaming array and covert from string to integer - php

I have var_dump() variable $fruit_array as below:
array (size=3)
0 =>
array (size=1)
'fruit_id' => string '2' (length=1)
1 =>
array (size=1)
'fruit_id' => string '1' (length=1)
2 =>
array (size=1)
'fruit_id' => string '3' (length=1)
I need to rename fruit_id to id and convert array string value to integer, example result:
array (size=3)
0 =>
array (size=1)
'id' => int 2
1 =>
array (size=1)
'id' => int 1
2 =>
array (size=1)
'id' => int 3
How can I do that ? thanks

You can do something like this:
<?php
foreach ( $array as $k=>$v )
{
$array[$k] ['id'] = intval( $array[$k] ['fruit_id'] );
unset($array[$k]['fruit_id']);
}

Related

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);

Sort multidimensonal array by version

Does anyone know how to correctly sort this array by version preserving the sub-arrays.
I have seen similar issues and solutions on stackoverflow but im not able to apply to my array.
Thanks in advance
array (size=3)
'1.23.006' =>
array (size=1)
0 => string '1' (length=1)
'2.0.0' =>
array (size=1)
0 => string '1' (length=1)
'10.0.0' =>
array (size=2)
0 => string '1' (length=1)
1 => string '4' (length=1)
I want it to be sorted like this:
array (size=3)
'10.0.0' =>
array (size=2)
0 => string '1' (length=1)
1 => string '4' (length=1)
'2.0.0' =>
array (size=1)
0 => string '1' (length=1)
'1.23.006' =>
array (size=1)
0 => string '1' (length=1)
You need to use uksort to sort your array by keys, with a custom function that calls version_compare to compare them:
$data = array('1.23.006' => array('1'), '2.0.0' => array('1'), '10.0.0' => array('1', '4'));
uksort($data, function ($a, $b) {
return version_compare($b, $a);
});
print_r($data);
Output:
Array
(
[10.0.0] => Array
(
[0] => 1
[1] => 4
)
[2.0.0] => Array
(
[0] => 1
)
[1.23.006] => Array
(
[0] => 1
)
)
Demo on 3v4l.org

how to use mysql_num with somfony2

I know its a very silly question but having issues with symfony and doctrine createQuery..
I need an array result like this way to feed my chart app:
array (size=6)
0 =>
array (size=2)
0 => string 'Firefox' (length=7)
1 => float 45
1 =>
array (size=2)
0 => string 'IE' (length=2)
1 => float 26.8
2 =>
array (size=2)
0 => string 'Chrome' (length=6)
1 => float 12.8
and im using this query:
$query = $this->getEntityManager()->createQuery(
'SELECT c.status, COUNT (c.status) FROM MyAppBundle:Content c GROUP BY c.status '
);
$results = $query->getArrayResult();
but its returning like this:
array (size=4)
0 =>
array (size=2)
'status' => string 'Converted' (length=9)
1 => string '4359' (length=4)
1 =>
array (size=2)
'status' => string 'Received Q' (length=10)
1 => string '277' (length=3)
2 =>
array (size=2)
'status' => string 'Signed' (length=6)
1 => string '1' (length=1)
3 =>
array (size=2)
'status' => string 'Uploaded' (length=8)
1 => string '1' (length=1)
Now in regular php and mysql:
while($row = mysql_fetch_array($query,MYSQL_NUM)){
$results[] = $row ;
}
can return the array result as expected but my q is how can i do it with symfony2 and doctrine way?
You can give your arrays numeric indexes by using array_values
foreach($results as &$result) {
$result = array_values($result);
}

How to add to an array in a loop

I’m trying to add to an array in a loop but only the first element in the loop is added.
The array
array (size=7)
0 =>
array (size=2)
'id' => int 1
'name' => string 'john' (length=11)
1 =>
array (size=2)
'id' => int 2
'name' => string 'adam' (length=13)
2 =>
array (size=2)
'id' => int 3
'name' => string 'mary' (length=11)
My loop
foreach ($loops as $key => $loop) {
$idArray['id'] = $loop['id'];
}
var_dump($idArray); die();
Did I do anything wrong?
You overwrite your old value by assigning the new value to the array. An array can't have identical keys.
Try this:
foreach ($loops as $key => $loop)
{
$idArray['id'][] = $loop['id'];
}
var_dump($idArray); die();
So you add items to an array inside your array.
If you want the ordinal values of the $idarray to be the primary key values of what you are iterating over you can do this.
$loops = array(array('id' => 1, 'name' => 'john'), /* ... */);
foreach ($loops as $key => $loop)
{
$idArray[$loop['id']] = $loop;
}
var_dump($idArray); die();
var_dump will reveal this structure
array (size=7)
1 =>
array (size=2)
'id' => int 1
'name' => string 'john' (length=4)
2 =>
array (size=2)
'id' => int 2
'name' => string 'adam' (length=4)
3 =>
array (size=2)
'id' => int 3
'name' => string 'mary' (length=4)

Illegal string offset in foreach

I'm trying to loop in the second array with this code
<?php
foreach ($categories[1] as $category_cat) { ?>
<li><span><?php echo $category_cat['name']; ?></span></li>
<?php } ?>
And I get the "Illegal string offset in..." error. I know that addind the [1] the the foreach might be the error but how I could make the loop in the second array then ?
Array content
array (size=2)
0 =>
array (size=4)
'name' => string 'Brands' (length=7)
'children' =>
array (size=12)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
2 =>
array (size=3)
...
3 =>
array (size=3)
...
4 =>
array (size=3)
...
5 =>
array (size=3)
...
6 =>
array (size=3)
...
7 =>
array (size=3)
...
8 =>
array (size=3)
...
9 =>
array (size=3)
...
10 =>
array (size=3)
...
11 =>
array (size=3)
...
'column' => string '1' (length=1)
'href' => string 'url here' (length=78)
1 =>
array (size=4)
'name' => string 'Catégories' (length=11)
'children' =>
array (size=7)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
Just try to loop $categories[1]['children']:
foreach ( $categories[1]['children'] as $category_cat ) { }

Categories