This is my array:
Array
(
[0] => Array
(
[entity_id] => 1
[value] => new
[label] => New
)
[1] => Array
(
[entity_id] => 3
[value] => pending_payment
[label] => Pending Payment
)
[2] => Array
(
[entity_id] => 4
[value] => pending_paypal
[label] => Pending Paypal
)
[3] => Array
(
[entity_id] => 5
[value] => processing
[label] => Processing
)
[4] => Array
(
[entity_id] => 6
[value] => complete
[label] => Complete
)
[5] => Array
(
[entity_id] => 7
[value] => canceled
[label] => Canceled
)
[6] => Array
(
[entity_id] => 8
[value] => closed
[label] => Closed
)
[7] => Array
(
[entity_id] => 9
[value] => holded
[label] => Holded
)
[8] => Array
(
[entity_id] => 10
[value] => payment_review
[label] => Payment Review
)
[9] => Array
(
[entity_id] => 11
[value] => new
[label] => New
)
[10] => Array
(
[entity_id] => 13
[value] => pending_payment
[label] => Pending Payment
)
UPDATE This is the result of the print_r.
As you can see the array[0] with array[6] are the same. Also the array[7] with the array[1]. How can I get rid of one of them ? thx
I tried smth like this:
$input = $my_array
$temp = $input;
foreach ( $temp as &$data ) {
unset($data['id']);
}
$output = array_intersect_key($input, array_unique($temp));
but with no any result :( .
$result = array();
foreach ($myArray as $array) {
if (isset($result[$array['value']])) {
continue;
}
$result[$array['value']] = $array;
}
print_r($result);
Hey Attila this should help.
Your $my_array is renamed in this example into $wholeArray. And I thought it woulf be enough to compare the labels:
$index=0;
$length = count($wholeArray);
foreach($wholeArray as $arrayElement){
$temp = $arrayElement['label'];
$index++;
for($i = $index; $i < $length; $i++){
if($wholeArray[$i]['label'] == $temp){
unset($wholeArray[$i]);
$length --;
}
}
}
If you have any questions to the code feel free to ask :)
Related
Basically i want to loop through a multidimensional associative array to get a simple indexed array
Here is my master array
Array
(
[0] => Array
(
[user_id] => 2
[children] => Array
(
[0] => Array
(
[user_id] => 5
[children] => Array
(
)
)
[1] => Array
(
[user_id] => 6
[children] => Array
(
)
)
)
)
[1] => Array
(
[user_id] => 3
[children] => Array
(
[0] => Array
(
[user_id] => 7
[children] => Array
(
)
)
[1] => Array
(
[user_id] => 8
[children] => Array
(
)
)
)
)
[2] => Array
(
[user_id] => 4
[children] => Array
(
[0] => Array
(
[user_id] => 9
[children] => Array
(
[0] => Array
(
[user_id] => 10
[children] => Array
(
[0] => Array
(
[user_id] => 11
[children] => Array
(
)
)
[1] => Array
(
[user_id] => 12
[children] => Array
(
)
)
[2] => Array
(
[user_id] => 13
[children] => Array
(
)
)
)
)
)
)
)
)
)
Here is the result i want to achive
$userArray= array(2,3,4,5,6,7,8,9,10,11,12,13);
basically i just want all the user_id key value inside 1 single indexed array.
Till now i have tried this code
$keys = array_keys($masterArray);
for($i = 0; $i < count($masterArray); $i++) {
echo $keys[$i] . "{<br>";
foreach($masterArray[$keys[$i]] as $key => $value) {
echo $key . " : " . $value . "<br>";
}
echo "}<br>";
}
You can use array_walk_recursive() which will iterate over the leaf nodes in a multidimensional array, check for the key being user_id and if so, add it to a list of ids...
$ids = [];
array_walk_recursive($masterArray, function ( $value, $key) use (&$ids) {
if ( $key == "user_id" ) {
$ids[] = $value;
}
});
print_r($ids);
which with the sample data, gives...
Array
(
[0] => 2
[1] => 5
[2] => 6
[3] => 3
[4] => 7
[5] => 8
[6] => 4
[7] => 9
[8] => 10
[9] => 11
[10] => 12
[11] => 13
)
You just have to travel recursively to collect all user IDs as below:
<?php
function collectUserIDs($data,&$result){
foreach($data as $current_data){
$result[] = $current_data['user_id'];
collectUserIDs($current_data['children'],$result);
}
}
$result = [];
collectUserIDs($data,$result);
print_r($result);
This question already has answers here:
PHP array delete by value (not key)
(20 answers)
Closed 3 years ago.
Following is my array and i want to remove specific value from array.
Array
(
[0] => Array
(
[name] => categoryfilter
[value] => 127
)
[1] => Array
(
[name] => price_min
[value] => sd
)
[2] => Array
(
[name] => price_max
[value] => sdsd
)
[3] => Array
(
[name] => action
[value] => myfilter
)
[4] => Array
(
[name] => quantity
[value] => 1
)
[5] => Array
(
[name] => quantity
[value] => 1
)
[6] => Array
(
[name] => quantity
[value] => 1
)
[7] => Array
(
[name] => quantity
[value] => 0
)
[8] => Array
(
[name] => quantity
[value] => 0
)
[9] => Array
(
[name] => quantity
[value] => 0
)
[10] => Array
(
[name] => quantity
[value] => 1
)
);
I want to remove all quantity key items from array .
I have tried using following way but not remove display same thing.
if (($key = array_search('quantity', $_POST['product'])) !== false) {
unset($_POST['product'][$key]);
}
echo "<pre>";print_r($_POST['product']);
Loop through the array.
foreach ($_POST['product'] as $k => $p) {
if ($p['name'] == 'quantity') {
unset($_POST['product'][$k];
}
}
Use array_filter with callback function
$f = array_filter($a, function($v){return $v['name'] != 'quantity';});
Working example : https://3v4l.org/DqoLj
Hi guys I was wondering how can I add a incremental all elements? Because as of now I am not sure where can I include the "inc" in all elements
Ex:
MY_ARRAY = (
[id] => 4
[children] => Array
(
[0] => Array
(
[id] => 18
[children] => Array
(
[0] => Array
(
[id] => 21
)
[1] => Array
(
[id] => 22
)
)
)
[1] => Array
(
[id] => 19
)
[2] => Array
(
[id] => 20
[children] => Array
(
[0] => Array
(
[id] => 26
)
)
)
)
)
Using these code:
$in = MY_ARRAY
function generateArray($in, $parent = 0){
foreach ($in as $key => $value) {
if(is_numeric($key)){
$in = $value;
$out[$key] = $this->generateArray($in, $parent);
}else{
$out[$key]=$value;
if($key=="id"){
$out['p_id'] = $parent;
$parent=$value;
}elseif($key=="children"){
$in = $value;
$out[$key] = $this->generateArray($in, $parent);
}
}
}
return $out;
}
Will give me this output, not including the [inc].
[id] => 4
[P_id] => 0
[inc] => 1
[children] => Array
(
[0] => Array
(
[id] => 18
[P_id] => 4
[inc] => 2
[children] => Array
(
[0] => Array
(
[id] => 21
[P_id] => 18
[inc] => 3
)
[1] => Array
(
[id] => 22
[P_id] => 18
[inc] => 4
)
)
)
[1] => Array
(
[id] => 19
[P_id] => 4
[inc] => 5
)
[2] => Array
(
[id] => 20
[P_id] => 4
[inc] => 6
[children] => Array
(
[0] => Array
(
[id] => 26
[P_id] => 20
[inc] => 7
)
)
)
)
)
Now I'm not sure where and how can I include the [inc] or the incremental value of each element in the array using my code above.
Need really help here guys...
I'm have really lame question about PHP arrays, I'm trying to get all values of form[extras][], in this case they are 1,2,3,4, here is the output of print_r($array):
Array
(
[0] => Array
(
[name] => form[pickupDate][day]
[value] => 1
)
[1] => Array
(
[name] => form[pickupDate][month]
[value] => 10
)
[2] => Array
(
[name] => form[pickupTime][hour]
[value] => 0
)
[3] => Array
(
[name] => form[returnDate][day]
[value] => 1
)
[4] => Array
(
[name] => form[returnDate][month]
[value] => 1
)
[5] => Array
(
[name] => form[returnTime][hour]
[value] => 0
)
[6] => Array
(
[name] => form[car]
[value] => 1
)
[7] => Array
(
[name] => form[pickupAddress]
[value] =>
)
[8] => Array
(
[name] => form[agency]
[value] => 1
)
[9] => Array
(
[name] => form[extras][]
[value] => 1
)
[10] => Array
(
[name] => form[extras][]
[value] => 2
)
[11] => Array
(
[name] => form[extras][]
[value] => 3
)
[12] => Array
(
[name] => form[extras][]
[value] => 4
)
[13] => Array
(
[name] => form[specialPrice]
[value] =>
)
)
You can play with my data, by using this JSON string after converting it to PHP array like this:
$request = '[{"name":"form[pickupDate][day]","value":"1"},{"name":"form[pickupDate][month]","value":"8"},{"name":"form[pickupTime][hour]","value":"0"},{"name":"form[returnDate][day]","value":"1"},{"name":"form[returnDate][month]","value":"1"},{"name":"form[returnTime][hour]","value":"0"},{"name":"form[pickupAddress]","value":""},{"name":"form[agency]","value":"1"},{"name":"form[extras][]","value":"1"},{"name":"form[extras][]","value":"2"},{"name":"form[extras][]","value":"3"},{"name":"form[extras][]","value":"4"},{"name":"form[specialPrice]","value":""}]';
$array = json_decode($request,true);
I already tried with this, but it's resulting string(1) "4":
$result = array_column($array, null, 'name')['form[extras][]']['value'];
var_dump($result);
You have a multidimensional array, this is an array composed by several arrays.
array->[0]-[1]-[2]...
| |
| [name]-[value]
|
array-> [name]-[value]
That's why you have to go through each element, for instance with this function (you have one already defined in PHP > 5.5.0):
function array_column($array, $value)
{
$result = array();
foreach($array as $element)
{
$result[] = $element[$value];
}
return $result[];
}
Edit:
for getting the values of a subset of specific arrays based upon their name:
function getValuesOfTheArraysForExtras($array)
{
$result = array();
foreach($array as $element)
{
if($element['name']=='form[extras][]')
$result[] = $element['value'];
}
return $result;
}
I'm having a problem to sort multidimensional array.
print_r($myarray); is giving following output:
Array
(
[0] => Array
(
[ID] => 10
[Code] => 12-111
[Name] => putup1
)
[1] => Array
(
[ID] => 11
[Code] => 12-21
[Name] => putup2
)
[2] => Array
(
[ID] => 12
[Code] => 12-1
[Name] => putup2
)
)
I took a reference from this link: Sort MultiDimensional Array and sorted using column Code. it given me output like this:
Array
(
[0] => Array
(
[ID] => 10
[Code] => 12-1
[Name] => putup1
)
[1] => Array
(
[ID] => 12
[Code] => 12-111
[Name] => putup2
)
[2] => Array
(
[ID] => 11
[Code] => 12-21
[Name] => putup2
)
)
And I expecting output like :
Array
(
[0] => Array
(
[ID] => 10
[Code] => 12-1
[Name] => putup1
)
[2] => Array
(
[ID] => 11
[Code] => 12-21
[Name] => putup2
)
[1] => Array
(
[ID] => 12
[Code] => 12-111
[Name] => putup2
)
)
Is it possible? Please suggest me...
Thank you :)
You are currently sorting on code, but as text and not as a number. To sort it as you want, you have to write a comparison function that indicates whether one code is bigger than another:
function sortByCode($a, $b) {
$aParts = explode('-', $a['Code']);
$bParts = explode('-', $b['Code']);
for ($i = 0; $i < count($aParts); $i++) {
if ($aParts[$i] < $bParts[$i]) {
return -1;
} else if ($aParts[$i] > $bParts[$i]) {
return 1;
}
}
return 0;
}
usort($myArray, 'sortByCode');