How to remove parent array index from array - php

I want to remove parent array index in array.
Following is my array.
Array
(
[0] => Array
(
[0] => Array
(
[id] => 296
[username] => David0123
[profile_slug] => david-love
)
)
[1] => Array
(
[0] => Array
(
[id] => 297
[username] => Anne_wils
[profile_slug] => anne-chase
)
)
[2] => Array
(
[0] => Array
(
[id] => 300
[username] => malina001
[profile_slug] => malina-reid
)
)
)
And I want like this way..
Array(
[0] => Array
(
[id] => 296
[username] => David0123
[profile_slug] => david-love
)
[1] => Array
(
[id] => 297
[username] => Anne_wils
[profile_slug] => anne-chase
)
[2] => Array
(
[id] => 300
[username] => malina001
[profile_slug] => malina-reid
)
)
I used following script for it but not work.
$myMainArray = json_decode(json_encode($allEscorts),true);
$i=0;
foreach( array_values($myMainArray) as $k=> $val){
echo $val[$i]['id'];
$i++;
}
I want to display data each element but first i have to remove parent array indexes.

You can use array_map to pull values up one level
$myMainArray = json_decode(json_encode($allEscorts),true);
$myMainArray = array_map(function($el) {
return $el[0];
}, $myMainArray);

You should check if the first array could be generate as you wish.
If not you can use array_map to get the first index from the inner-array.
for example:
$result = array_map(function($item){
return $item[0]; // always return the first array-index
}, $first_array);

Related

how to add array value at specfic index in php codenighter

I have array value like this
Array
(
[0] => Array
(
[channel] => 15
[id] => clsrnMdVKq2omEuQabSCHp83ezAX6w
)
[1] => Array
(
[channel] => 16
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
[2] => Array
(
[channel] => 17
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
)
Now i want to add another array value in specific index .lets say i wants to add this array value at index 1
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxxewqeqwexxxxxxxewrewrw
)
Now the result output should be like this
Array
(
[0] => Array
(
[channel] => 15
[id] => clsrnMdVKq2omEuQabSCHp83ezAX6w
)
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxxewqeqwexxxxxxxewrewrw
)
[2] => Array
(
[channel] => 16
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
[3] => Array
(
[channel] => 17
[id] => MfSoHUKjD5n90EZbstpiRGY7e8cgh2
)
)
this is my foreach loop to serlize channel and id
foreach ($channel as $key => $ch) {
$user_hash['channel'] = json_encode($ch);
$user_hash['id'] = random_string('alnum', 30);
array_push($user_hash_array, $user_hash);
}
You need to split the array into 2, then insert your new value at the end of the first sub-array, then merge it with the second sub-array. EG: an array which looks like [1,3,4,5] and you want to insert "2" at position 2, then you split at position one to have [1] and [3,4,5]; then you append "2" at the end of first sub-array to form [1,2], then merge this new subarray with the other sub-array([3,4,5]) to form [1,2] + [3,4,5].
For your implementation, try this code:
$array = array() // the original array you want to modify
$insert = array() // the array you want to push into the original one above
$position = 1 // the position at which you want to insert the new item
$newArray = array_slice($array, 0, $position, TRUE) + $insert + array_slice($array, $position, NULL, TRUE);
you can use array_splice array method for add element in array at particular position
<?php
$original_array = array(
array("channel"=>15,"id"=>"sdfdfsf1"),
array("channel"=>16,"id"=>"sdfdfsf2"),
array("channel"=>17,"id"=>"sdfdfsf3")
);
echo "<pre>";print_r($original_array);
$inserted_element =
array(array("channel"=>20,"id"=>"xxxxxxxxxxewqeqwexxxxxxxewrewrw"));
$position=1;
array_splice( $original_array, $position, 0, $inserted_element );
echo "<pre>";print_r($original_array);
?>
Output will be as following
Array
(
[0] => Array
(
[channel] => 15
[id] => sdfdfsf1
)
[1] => Array
(
[channel] => 16
[id] => sdfdfsf2
)
[2] => Array
(
[channel] => 17
[id] => sdfdfsf3
)
)
Array
(
[0] => Array
(
[channel] => 15
[id] => sdfdfsf1
)
[1] => Array
(
[channel] => 20
[id] => xxxxxxxxxxewqeqwexxxxxxxewrewrw
)
[2] => Array
(
[channel] => 16
[id] => sdfdfsf2
)
[3] => Array
(
[channel] => 17
[id] => sdfdfsf3
)
)

How do i make one array out of two arrays containing different values

I have two arrays, one called fetched_services and one called fetched_companies, they look like this:
fetched_services
(
[1] => Array
(
[id] => 11
[child_services] => Array
(
[0] => Array
(
[id] => 153
)
[1] => Array
(
[id] => 137
)
[2] => Array
(
[id] => 138
)
)
)
)
fetched_companies
(
[0] => stdClass Object
(
[services] => Array
(
[0] => 25
[1] => 102
)
)
)
What i want to achieve is to end up with an array like fetched_services but only having child_services with id of fetched_companies["services"].
What i have tried is this:
$services = [];
$isFound = false;
foreach ($fetched_services as $fetched_service) {
foreach ($fetched_service["child_services"] as $fetched_child_service) {
$fetched_service["child_services"] = [];
foreach ($fetched_companies as $fetched_company) {
if( (in_array($fetched_child_service["id"],$fetched_company->services)) ) {
$fetched_service["child_services"][] = $fetched_child_service;
$isFound = true;
}
}
if($isFound) {
$services[] = $fetched_service;
$isFound = false;
}
}
}
This outputs this:
Array
(
[0] => Array
(
[id] => 11
[child_services] => Array
(
[0] => Array
(
[id] => 116
)
)
)
[1] => Array
(
[id] => 11
[child_services] => Array
(
[0] => Array
(
[id] => 117
)
)
)
)
As you can see the resulting array have two arrays containing same id but the child_services are different.
What i want to end up with is this:
Array
(
[0] => Array
(
[id] => 11
[child_services] => Array
(
[0] => Array
(
[id] => 116
)
)
(
[0] => Array
(
[id] => 117
)
)
)
)
What am i doing wrong here? Thanks!
Your question is a bit unclear (I'll edit/delete this if/when you clarify, see my comment above), but you could probably make use of array_filter by only keeping services that are present in said list (via in_array):
$result = array_filter(
$fetched_services['child_services'],
fn(array $child_service): bool => in_array($child_service['id'], $fetched_companies[0]->services, true)
);
Demo

json array sort by value in php

I have JSON arrays of objects. I am trying to sort an array using usort. I want to use value field from field_listing_order. It sorted using value. I am missing something but not able to figure it out. please review the code. Thanks!
stdClass Object
(
[node_title] => abc
[nid] => 2281
[field_api_order_value] => 201
[field_node_entity_type] => node
[_data] => Array
(
[nid] => Array
(
[entity_type] => node
[entity] => stdClass Object
(
[title] => abc
[field_listing_order] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 8
[format] =>
[safe_value] => 8
)
)
)
)
)
)
)
stdClass Object
(
[node_title] => abc
[nid] => 2243
[field_api_order_value] => 204
[field_node_entity_type] => node
[_data] => Array
(
[nid] => Array
(
[entity_type] => node
[entity] => stdClass Object
(
[title] => abc
[field_listing_order] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 3
[format] =>
[safe_value] => 3
)
)
)
)
)
)
) stdClass Object
(
[node_title] => abc
[nid] => 2431
[field_api_order_value] => 242
[field_node_entity_type] => node
[_data] => Array
(
[nid] => Array
(
[entity_type] => node
[entity] => stdClass Object
(
[title] => abc
[field_listing_order] => Array
(
[und] => Array
(
[0] => Array
(
[value] => 1
[format] =>
[safe_value] => 1
)
)
)
)
)
)
)
and So on ...
foreach ($view->result as $result) {
$node = $result->_data['nid']['entity'];
$listing_order = $node->field_listing_order[LANGUAGE_NONE][0];
.....
// code goes here and it works well. sorting issue
}
usort ($node->field_listing_order[LANGUAGE_NONE][0], function($a, $b){
return strcmp($a->value, $b->value);
}); ?>
If you need to sort all the nodes using the field_listing_order value, you need to compare the values along the full path, from the first object to the value:
usort($view->result, function($a, $b) {
$la = $a->_data['nid']['entity']->field_listing_order[LANGUAGE_NONE][0]['value'];
$lb = $b->_data['nid']['entity']->field_listing_order[LANGUAGE_NONE][0]['value'];
return $la - $lb ;
});
In this case $a and $b are two different nodes which could be compared. That why you should compare the field_listing_order's value of these nodes. The answer is working with $la- $lb

Collecting values from a multidimensional array using foreach loops

There is an array which is built looks like the following (with some more values which i left away for this example):
Array
(
[0] => Array
(
[id] => 44
[cars] => Array
(
[0] => Array
(
[id] => 38
)
[1] Array
(
[id] => 39
)
)
)
[1] => Array
(
[id] => 45
[cars] => Array
(
[0] => Array
(
[id] =>136
)
[1] =>Array
(
[id] =>137
)
[2] =>Array
(
[id] =>138
)
)
)
)
I want to build another array from the above in the following form:
Array
(
[0] => Array
(
['car_filter_sort_id'] => 44
['car_id'] => 38
)
[1] => Array
(
['car_filter_sort_id'] => 44
['car_id'] => 39
)
[2] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 136
)
[3] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 137
)
[4] => Array
(
['car_filter_sort_id'] => 45
['car_id'] => 138
)
)
I tried to achieve this with following function:
foreach($filterSortSaveArray as $filterSortSaveArray['cars'] => $value){
$id = $filterSortSaveArray['id'];
foreach($value['cars'] as $value => $car){
$field_values['car_filter_sort_id'] = $id;
$field_values['car_id'] = $car['id'];
}
}
But the the result differs from what I have expected. Any suggestions?
There are two big issues in your code. First, are you referencing undefined value with $filterSortSaveArray['cars'], since there is no 'cars' key in the first level of the original array. Second, by assigning values to $field_values['car_filter_sort_id'] and $field_values['car_id'] in the loop you are just overriding them in each iteration. You need to push the values into an array using []= operator (which is equivalent to applying array_push()).
Try this:
$result = [];
foreach($filterSortSaveArray as $k => $v) {
if (!is_array($v['cars']))
continue;
$id = $v['id'];
foreach ($v['cars'] as $i => $car){
$result[] = [
'car_filter_sort_id' => $id,
'car_id' => $car['id']
];
}
}

How to rationalise a multidimensional array in php?

I've been trying this all day!
How would I convert the top multidimensional array into the bottom.
Array (
[0] => Array ( [id] => 34 [email] => a#example.com )
[1] => Array ( [id] => 34 [email] => b#example.com )
[2] => Array ( [id] => 33 [email] => c#example.com )
[3] => Array ( [id] => 33 [email] => d#example.com )
[4] => Array ( [id] => 33 [email] => e#example.com )
)
Array (
[0]=>Array ([id] => 34 [email] => Array ([0]=> a#example.com [1]=>b#example.com )
[1]=>Array ([id] => 33 [email] => Array ([0]=> c#example.com [1]=>d#example.com [2]=>e#example.com)
)
Many thanks.
$new_array = array();
foreach ($orig_array as $child) {
$new_array[$child['id']][] = $child['email'];
}
$final_array = array();
foreach($new_array as $child) {
$final_array[] = $child;
}
The first loop produces an array keyed off the id fields, and simply pushes each email address onto it. The second loop then takes that intermediate array and wraps another array around it for the 0,1,etc... keys.
Would not just using keys in order to store IDs be an easier way to do that? Like this:
Array (
[34]=>Array ([email] => Array ([0]=> a#example.com [1]=>b#example.com )
[33]=>Array ([email] => Array ([0]=> c#example.com [1]=>d#example.com [2]=>e#example.com)
)
Then grouping emails would become a trivial task.

Categories