Multidimensional array to single array in PHP - php

I am working in PHP so I have an array like this, from this array I want filter take user_id to another array like I given below.
Array
(
[0] => Array
(
[user_id] => 66
[distance] => 0
)
[1] => Array
(
[user_id] => 68
[distance] => 0
)
[2] => Array
(
[user_id] => 81
[distance] => 0
)
[3] => Array
(
[user_id] => 65
[distance] => 0.00010218008081861118
)
)
I want an array like this,
$user_id=array(66,68,81,65);

Use array_column()
Returns an array of values representing a single column from the input array.
<?php
$user_array = array(
0 => array('user_id' => 1, 'name' => 'Bob'),
1 => array('user_id' => 2, 'name' => 'John'),
2 => array('user_id' => 3, 'name' => 'Mary')
);
$users = array_column($user_array, 'user_id');
print_r($users);
Output :
Array
(
[0] => 1
[1] => 2
[2] => 3
)

Where $array is the multidimensional array you provided above:
$data = array();
foreach ($array as $item) {
$data[] = $item['user_id'];
}
print_r($data);

Related

How to group posted array names by index in PHP

I am currently working on a project where the fields scale when clicking on the "Add" button.
I am grouping each field like this: name="packaging[]", name="packaging[1]", name="packaging[2]" and so on. When I submit the form, this is how the data looks like when posted:
Array
(
[packaging] => Array
(
[0] => 1
[1] => 2
)
[quantity] => Array
(
[0] => 1
[1] => 2
)
[total-weight] => Array
(
[0] => 1
[1] => 2
)
[length] => Array
(
[0] => 1
[1] => 2
)
)
Using PHP I would like to convert the above code to look like this:
Array
(
[0] => Array
(
[packaging] => 1,
[quantity] => 1,
[total-weight] => 1,
[length] => 1,
)
[1] => Array
(
[packaging] => 2,
[quantity] => 2,
[total-weight] => 2,
[length] => 2,
)
)
Any help would be greatly appreciated.
Try this....
$array=array();
foreach($data as $key=>$value){
foreach($value as $k=>$val){
$array[$k][$key]=$val;
}
}
DEMO
Try this code:
$rows = array ('packaging' => array ('0'=> 1,'1' => 2),'quantity' => array('0'=> 1,'1' => 2),'total-weight' => array ('0'=> 1,'1' => 2),
'length' =>array ('0'=> 1,'1' => 2)
);
$res_array = array();
$total_records = count($rows['packaging']);
for($i=0;$i<$total_records;$i++)
{
$res_array[] = array('packaging'=>$rows['packaging'] [$i],'quantity'=>$rows['quantity'][$i],
'total-weight'=>$rows['total-weight'][$i],'length'=>$rows['length'] [$i]);
}
print_r($res_array);

Remove specific string from whole associative array items

I have an associative array. I want to remove specific string from whole array items. Here is the structure of my array:
Array
(
[50808] => Array
(
[quantity] => 2
[name] => asv (test)
)
[50809] => Array
(
[quantity] => 2
[name] => 37 (test)
)
[50810] => Array
(
[quantity] => 3
[name] => 38 (test)
)
)
Output i want:
Array
(
[50808] => Array
(
[quantity] => 2
[name] => asv
)
[50809] => Array
(
[quantity] => 2
[name] => 37
)
[50810] => Array
(
[quantity] => 3
[name] => 38
)
)
I know its very simple using loop but i want to do it without loop.
Use array_map function
<?php
$array = Array
(
'50808' => Array
(
'quantity' => 2,
'name' => 'asv (test)',
),
'50809' => Array
(
'quantity' => 2,
'name' => '37 (test)'
),
'50810' => Array
(
'quantity' => 3,
'name' => '38 (test)'
)
);
echo '<pre>';
$new_array = array_map(function($val){
$val['name'] = trim(str_replace('(test)', '', $val['name']));
return $val;
}, $array);
print_r($new_array);
Output:
Array
(
[50808] => Array
(
[quantity] => 2
[name] => asv
)
[50809] => Array
(
[quantity] => 2
[name] => 37
)
[50810] => Array
(
[quantity] => 3
[name] => 38
)
)
Another method can be array_walk function:
array_walk($array, function(&$val){
$val['name'] = trim(str_replace('(test)', '', $val['name']));
});
print_r($array); //out put: desired output

PHP Compare two nested arrays to get a new array

I am trying to subtract parts of one nested array from another, but I'm having difficulty specifying the parts that I want to subtract as both values are numbers.
My arrays are, for example:
Array ( [0] => Array ( [id] => 43 [quantity] => 4 ) )
Array ( [0] => Array ( [id] => 43 [quantity] => 2 ) )
And after the subtraction I want the Result to be:
Array ( [0] => Array ( [id] => 43 [quantity] => 2 ) )
I'm using the following code to perform the subtraction, but I can't stop it from subtracting the id from itself:
foreach(array_keys($arrayA) as $id)
{
foreach(array_keys($arrayA[$id]) as $type)
{
$newArray[$id][$type] = $arrayA[$id][$type] - $arrayB[$id][$type];
}
}
print_r($newArray);
Could someone please tell me how I can just effect the [quantity] part of the array, without changing the [id]? With the code as it is I get:
Array ( [0] => Array ( [id] => 0 [quantity] => 2 ) )
Thanks in advance.
$ar1 = array(0 => array('id' => 43, 'quantity' => 4));
$ar2 = array(0 => array('id' => 43, 'quantity' => 2));
$new_array = array();
foreach($ar1 as $key => $value)
{
$new_array[$key] = array('id' => $value['id'], 'quantity' => ($value['quantity'] - $ar2[$key]['quantity']));
}
Array
(
[0] => Array
(
[id] => 43
[quantity] => 2
)
)

building a multidimensional array from mysql with php

I am trying to form a specific multidimensional array from a mysql result set.
I would like it to look like this:
array(
'product_name' => 'prod_1',
'categories' => array(1,2,3,4)
);
The db result return an array that looks something like this
Array
(
[0] => Array
(
[id] => 1
[product_name] => prod_1
)
[1] => Array
(
[id] => 2
[product_name] => prod_1
)
[2] => Array
(
[id] => 3
[product_name] => prod_1
)
[3] => Array
(
[id] => 4
[product_name] => prod_1
)
As you can see, i would like to group the product name and place the id into another array
Does anyone have any tips on how to do this?
$yourarray = array(array('id'=>1, 'product_name' => 'prod_1'), array('id'=>2, 'product_name' => 'prod_2'),array('id'=>3, 'product_name' => 'prod_3'));
$multiarray = array();
for($i=0; $i<count($yourarray);$i++){
$multiarray['product_name'][] = $yourarray[$i]['product_name'];
$multiarray['product_id'][] = $yourarray[$i]['id'];
}
print_r($yourarray); //original array
print_r($multiarray); //gives you multi array
Something similar to this?
Your original array:
Array
(
[0] => Array
(
[id] => 1
[product_name] => prod_1
)
[1] => Array
(
[id] => 2
[product_name] => prod_2
)
[2] => Array
(
[id] => 3
[product_name] => prod_3
)
)
The result would print:
Array
(
[product_name] => Array
(
[0] => prod_1
[1] => prod_2
[2] => prod_3
)
[product_id] => Array
(
[0] => 1
[1] => 2
[2] => 3
)
)
Did you try GROUP_CONCAT. It's something like:
SELECT name, GROUP_CONCAT(name) AS friends FROM friendships GROUP BY name;
Have a look for details here: http://forums.mysql.com/read.php?10,287931,287936#msg-287936
$yourarray = array(array('id'=>1, 'product_name' => 'prod_1'), array('id'=>2, 'product_name' => 'prod_1'), array('id'=>3, 'product_name' => 'prod_1'),array('id'=>4, 'product_name' => 'prod_1'));
$multiarray = array();
foreach ($yourarray as $value) {
if(!isset($multiarray['product_name'])) {
$multiarray['product_name'] = $value['product_name'];
}
$multiarray['categories'][] = $value['id'];
}
print_r($multiarray);

Remove duplicate elements off a multi-dimension array

I have an array contain this data
Array
(
[id] => Array
(
[0] => 1
[1] => 10
[2] => 4
)
[age] => Array
(
[0] => 1
[1] => 1
[2] => 2
)
)
Now I want to remove duplicates from the ['age'] and leave the first one in tact.
So this would return
Array
(
[id] => Array
(
[0] => 1
[2] => 4
)
[age] => Array
(
[0] => 1
[2] => 2
)
)
Any ideas? Or is there a function already in place to do this?
Like Gordon said, you'd need a custom function to make the relationship but you can use http://php.net/manual/en/function.array-unique.php ?
Wouldn't it be better to have the keys of the age array the corresponding values of the id array?
<?php
$array = array(
'id' => array(0 => 1, 1 => 10, 3 => 4),
'age' => array(0 => 1, 1 => 1, 2 => 2)
);
array_walk($array, 'dupe_killer');
print_r($array);
function dupe_killer(&$value, $key)
{
$value = array_unique($value);
}
?>
You could try this
$array = array('id' => array(1,10,4), 'age'=>array(1,1,2));
$age_array = array();
foreach ($array['age'] as $key => $val) {
if (in_array($val, $age_array))
unset($array['id'][$key], $array['age'][$key]);
$age_array[] = $val;
}
print_r($array);
this returns Array ( [id] => Array ( [0] => 1 [2] => 4 ) [age] => Array ( [0] => 1 [2] => 2 ) )
Regards
Luke

Categories