How to get specific values out of all indexes in an array - php

I have an array which I want to iterate over to push the items into a select box, but I can't figure out how to do it.
The array I get from the function:
array(2) {
["de"]=> array(10) {
["id"]=> int(10)
["order"]=> int(1)
["slug"]=> string(2) "de"
["locale"]=> string(5) "de-DE"
["name"]=> string(7) "Deutsch"
["url"]=> string(34) "http://localhost/werk/Mol/de/haus/"
["flag"]=> string(66) "http://localhost/werk/Mol/wp-content/plugins/polylang/flags/de.png"
["current_lang"]=> bool(false)
["no_translation"]=> bool(false)
["classes"]=> array(4) {
[0]=> string(9) "lang-item"
[1]=> string(12) "lang-item-10"
[2]=> string(12) "lang-item-de"
[3]=> string(15) "lang-item-first"
}
}
["nl"]=> array(10) {
["id"]=> int(3)
["order"]=> int(2)
["slug"]=> string(2) "nl"
["locale"]=> string(5) "nl-NL"
["name"]=> string(10) "Nederlands"
["url"]=> string(26) "http://localhost/werk/Mol/"
["flag"]=> string(66) "http://localhost/werk/Mol/wp-content/plugins/polylang/flags/nl.png"
["current_lang"]=> bool(true)
["no_translation"]=> bool(false)
["classes"]=> array(4) {
[0]=> string(9) "lang-item"
[1]=> string(11) "lang-item-3"
[2]=> string(12) "lang-item-nl"
[3]=> string(12) "current-lang"
}
}
}
I tried a foreach but I did only get the indexes of the array
<?php
$translations = pll_the_languages(array('raw' => 1));
$lang_codes = array();
foreach ($translations as $key => $value) {
array_push($lang_codes, $key);
}
?>
I need the language slug, URL, and flag from all indexes in this array (de & nl), what should I do?

A simple iteration over the outer array and then pick the values you want from the sub array.
<?php
$translations = pll_the_languages(array('raw' => 1));
$lang_codes = array();
foreach ($translations as $lang => $info) {
$lang_codes[$lang] = [ 'slug' => $info['slug'],
'url' => $info['url'],
'flag' => $info['flag']
];
}
?>

You can approach this as
$res = [];
foreach($translations as $key => $value){
$res[$key] = [
'slug' => $value['slug'],
'url' => $value['url'],
'flag' => $value['flag']
];
}
Live Demo

Related

Restructure an array of events in PHP combining attendees with common location

I have a PHP array that holds the dates, locations and time a person will be attending an event. It starts in this format (The original array is always delivered in date/time order):
array(2) {
[0]=>
array(2) {
["date"]=>
string(8) "1st June"
["events"]=>
array(4) {
[0]=>
array(3) {
["location"]=>
string(7) "Venue A"
["name"]=>
string(4) "Pete"
["time"]=>
string(6) "4.00pm"
}
[1]=>
array(3) {
["location"]=>
string(7) "Venue A"
["name"]=>
string(4) "John"
["time"]=>
string(6) "4.30pm"
}
[2]=>
array(3) {
["location"]=>
string(7) "Venue B"
["name"]=>
string(5) "Chris"
["time"]=>
string(6) "7.30pm"
}
[3]=>
array(3) {
["location"]=>
string(7) "Venue A"
["name"]=>
string(4) "Mark"
["time"]=>
string(6) "8.00pm"
}
}
}
[1]=>
array(2) {
["date"]=>
string(8) "2nd June"
["events"]=>
array(4) {
[0]=>
array(3) {
["location"]=>
string(7) "Venue B"
["name"]=>
string(4) "Fred"
["time"]=>
string(6) "5.00pm"
}
[1]=>
array(3) {
["location"]=>
string(7) "Venue C"
["name"]=>
string(5) "Boris"
["time"]=>
string(6) "6.00pm"
}
[2]=>
array(3) {
["location"]=>
string(7) "Venue A"
["name"]=>
string(6) "Rupert"
["time"]=>
string(6) "7.00pm"
}
[3]=>
array(3) {
["location"]=>
string(7) "Venue A"
["name"]=>
string(5) "David"
["time"]=>
string(6) "9.00pm"
}
}
}
}
What I need to do is combine this so attendees at the same location are grouped together where another attendee in a different location doesn't break up the list. So the above array would be changed to this:
array(2) {
[0]=>
array(2) {
["date"]=>
string(8) "1st June"
["events"]=>
array(3) {
[0]=>
array(2) {
["location"]=>
string(7) "Venue A"
["attendees"]=>
array(2) {
[0]=>
array(2) {
["name"]=>
string(4) "Pete"
["time"]=>
string(6) "4.00pm"
}
[1]=>
array(2) {
["name"]=>
string(4) "John"
["time"]=>
string(6) "4.30pm"
}
}
}
[1]=>
array(2) {
["location"]=>
string(7) "Venue B"
["attendees"]=>
array(1) {
[0]=>
array(2) {
["name"]=>
string(5) "Chris"
["time"]=>
string(6) "7.30pm"
}
}
}
[2]=>
array(2) {
["location"]=>
string(7) "Venue A"
["attendees"]=>
array(1) {
[0]=>
array(2) {
["name"]=>
string(4) "Mark"
["time"]=>
string(6) "8.00pm"
}
}
}
}
}
[1]=>
array(2) {
["date"]=>
string(8) "2nd June"
["events"]=>
array(3) {
[0]=>
array(2) {
["location"]=>
string(7) "Venue B"
["attendees"]=>
array(1) {
[0]=>
array(2) {
["name"]=>
string(4) "Fred"
["time"]=>
string(6) "5.00pm"
}
}
}
[1]=>
array(2) {
["location"]=>
string(7) "Venue C"
["attendees"]=>
array(1) {
[0]=>
array(2) {
["name"]=>
string(5) "Boris"
["time"]=>
string(6) "6.00pm"
}
}
}
[2]=>
array(2) {
["location"]=>
string(7) "Venue A"
["attendees"]=>
array(2) {
[0]=>
array(2) {
["name"]=>
string(6) "Rupert"
["time"]=>
string(6) "7.00pm"
}
[1]=>
array(2) {
["name"]=>
string(5) "David"
["time"]=>
string(6) "9.00pm"
}
}
}
}
}
}
So here's how I've figured out to do it juggling variables and stepping through the original array:
// $eventsArray holds the original array data a per the first example
$newArray = array();
foreach($eventsArray as $day) {
$curLocation = '';
$todaysEvents = array();
$teKey = -1;
foreach($day['events'] as $event) {
if($curLocation==$event['location']) {
$todaysEvents[$teKey]['attendees'][] = array(
'name' => $event['name'],
'time' => $event['time']
);
} else {
$todaysEvents[] = array(
'location' => $event['location'],
'attendees' => array(
array(
'name' => $event['name'],
'time' => $event['time']
)
)
);
$teKey = $teKey + 1;
}
$curLocation=$event['location'];
}
$newArray[] = array(
'date' => $day['date'],
'events' => $todaysEvents
);
}
And this works! So why am I asking a question on Stack? Because this feels awfully clunky and I'm not convinced there isn't a much more efficient way of doing it. I get the feeling I'm adding overhead here where it's not necessary.
Here's an alternative version using some PHP 7 (7.3+) features:
$resultEvents = [];
foreach ($events as ['date' => $date, 'events' => $dayEvents]) {
$resultDayEvents = ['date' => $date, 'events' => []];
foreach ($dayEvents as ['location' => $location, 'name' => $name, 'time' => $time]) {
$attendee = ['name' => $name, 'time' => $time];
if ($location === ($previousLocation ?? null)) {
$resultDayEvents['events'][array_key_last($resultDayEvents['events'])]['attendees'][] = $attendee;
} else {
$resultDayEvents['events'][] = ['location' => $location, 'attendees' => [$attendee]];
}
$previousLocation = $location;
}
$resultEvents[] = $resultDayEvents;
}
Demo: https://3v4l.org/bC4LK
You can use references like a cursor to keep track of the last event processed. Less effort to compare location and add attendees. But that comes with the complexity of getting rid of it properly.
Also, but a minor thing is to use strict comparisons... Can avoid some frustrating moments by being more predectible...
// $eventsArray holds the original array data a per the first example
$newArray = array();
foreach($eventsArray as $day) {
$todaysEvents = array();
$curEvent = null;
foreach($day['events'] as $event) {
if($curEvent===null || $curEvent['location']!==$event['location']) {
unset($curEvent);
$curEvent = array(
'location' => $event['location'],
'attendees' => array()
);
$todaysEvents[] = &$curEvent;
}
$curEvent['attendees'][] = array(
'name' => $event['name'],
'time' => $event['time']
);
}
unset($curEvent);
$newArray[] = array(
'date' => $day['date'],
'events' => $todaysEvents
);
}

combine same index of array?

I have following $_POST array
array(5) {
["addcatagory"]=>
string(8) "CATEGORY"
["reg_admin_id"]=>
string(2) "25"
["subcatagory"]=>
array(2) {
[0]=>
string(9) "SUB CAT 1"
[1]=>
string(9) "sub cat 2"
}
["subCat_Detais"]=>
array(2) {
[0]=>
string(9) "AAAAAAAAA"
[1]=>
string(8) "BBBBBBBB"
}
["submit"]=>
string(15) "Submit Catagory"
}
and
array(1) {
["subCatFile1"]=>
array(5) {
["name"]=>
array(3) {
[0]=>
string(5) "2.jpg"
[1]=>
string(5) "3.jpg"
[2]=>
string(0) ""
}
["type"]=>
array(3) {
[0]=>
string(10) "image/jpeg"
[1]=>
string(10) "image/jpeg"
[2]=>
string(0) ""
}
["tmp_name"]=>
array(3) {
[0]=>
string(18) "/var/tmp/phpN5ENy2"
[1]=>
string(18) "/var/tmp/phpRyJdcc"
[2]=>
string(0) ""
}
["error"]=>
array(3) {
[0]=>
int(0)
[1]=>
int(0)
[2]=>
int(4)
}
["size"]=>
array(3) {
[0]=>
int(65101)
[1]=>
int(49550)
[2]=>
int(0)
}
}
}
now what i want to achieve is combine 0 index of subcatagory and subcat_details in one array and of 1 index of subcatagory and subcat_details in second array and so on...
how can i achieve this?? is it even possible??
Expectations
array( 'name' => 'SUB CAT 1',
'details' => 'AAAAAAAAA',
'image_name'=>'2.jpg'
);
array( 'name' => 'SUB CAT 2',
'details' => 'BBBBBBB',
'image_name'=>'2.jpg'
);
This can be done with a simple foreach() loop -
$newArray = [];
foreach($_POST["subcatagory"] as $key => $value) {
$newArray[] = array("name" => $_POST["subcatagory"][$key],
"details" => $_POST["subCat_Detais"][$key]);
}
As #CharlotteDunois mentioned, you could also use an for() loop, as long as you have sequential keys, with no keys missing -
$newArray = [];
for($i=0;$i<count($_POST["subcatagory"]);$i++) {
$newArray[] = array("name" => $_POST["subcatagory"][$i],
"details" => $_POST["subCat_Detais"][$i]);
}

How to merge PHP multidimensional array keeping a unique key and separating with comma the other value

I have a multidimensional array and I want to make it matching the "unique key" value but merge the other key that has the same "unique key" value, it could be speared by comma, since my final output will be to use json_encode.
So for instance, if I have:
[0]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string(8) "123"
}
[1]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string(8) "124"
}
[2]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string(8) "126"
}
[3]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string(8) "129"
}
[4]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string(8) "130"
}
[5]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string(8) "102"
}
[6]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string(8) "193"
}
[7]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string(8) "156"
}
[8]=>
array(2) {
["label"]=>
string(2) "BG"
["value"]=>
string(8) "246"
}
[9]=>
array(2) {
["label"]=>
string(1) "C"
["value"]=>
string(8) "234"
}
[10]=>
array(2) {
["label"]=>
string(1) "C"
["value"]=>
string(8) "235"
}
[11]=>
array(2) {
["label"]=>
string(2) "CA"
["value"]=>
string(8) "345"
}
[12]=>
array(2) {
["label"]=>
string(2) "CA"
["value"]=>
string(8) "564"
}
And I want an output like:
[0]=>
array(2) {
["label"]=>
string(2) "AB"
["value"]=>
string "123,124,126,129,130,102,193,156“
}
[1]=>
array(2) {
["label"]=>
string(2) "BG"
["value"]=>
string "246"
}
[2]=>
array(2) {
["label"]=>
string(1) "C"
["value"]=>
string(8) "234,235,”
}
[3]=>
array(2) {
["label"]=>
string(2) "CA"
["value"]=>
string(8) "345,564,”
}
I am not sure how to do it, I've looked into array_merge_recursive and other similar solutions, but I did not got it, maybe I need to use implode.
You can use something like this:
$result = array();
foreach ($array as $arr) {
if (!isset($result[$arr['label']])) {
$result[$arr['label']] = $arr;
continue;
}
$result[$arr['label']]['value'] .= ',' . $arr['value'];
}
// if you really need numeric indexes use:
$result = array_values($result);
You can try
$array = array(
array(
'label' => "AB",
'value' => 123
),
array(
'label' => "AB",
'value' => 124
),
array(
'label' => "AB",
'value' => 125
),
array(
'label' => "C",
'value' => 126
),
array(
'label' => "C",
'value' => 127
),
array(
'label' => "C",
'value' => 127
),
);
$result = array();
foreach ($array as $el) {
$result[$el['label']] = array(
'label' => $el['label'],
'value' => implode( ',',
array_unique(
array_merge(
array_filter(
explode(',', $result[$el['label']]['value'])
),
array($el['value'])
)
)
)
);
}
echo "<pre>"; var_dump(array_values($result));

How to filter array to get only the object and not array in PHP

I'm not even sure what to search for for this question. What I really want is I have an array of objects like this
array(3) {
[0]=>
object(stdClass)#423 (4) {
["name"]=>
string(3) "Blah"
["full_name"]=>
string(10) "/Blah"
["id"]=>
string(32) "BlahBlah"
["parent_id"]=>
string(32) "BlahBlah"
}
[1]=>
object(stdClass)#422 (4) {
["name"]=>
string(8) "Blah1"
["full_name"]=>
string(9) "Blah2"
["id"]=>
string(32) "BlahBlah2"
["parent_id"]=>
NULL
}
[2]=>
object(stdClass)#421 (4) {
["name"]=>
string(4) "Blah3"
["full_name"]=>
string(11) "Blah3"
["id"]=>
string(32) "BlahBlah3"
["parent_id"]=>
string(32) "BlahBlahBlah3"
}
}
I want to filter to just the object that I want so what I did was
$found_label = array_filter($labels, function($obj) use($label) {
return $obj->name === $label;
});
But then the results I got is this
array(1) {
[1]=>
object(stdClass)#422 (4) {
["name"]=>
string(8) "Blah1"
["full_name"]=>
string(9) "Blah1"
["id"]=>
string(32) "BlahBlah2"
["parent_id"]=>
NULL
}
}
But what I really want is just this
object(stdClass)#422 (4) {
["name"]=>
string(8) "Blah1"
["full_name"]=>
string(9) "Blah1"
["id"]=>
string(32) "BlahBlah2"
["parent_id"]=>
NULL
}
Then I have to do this to just get the actual object
$theKey = key($found_label);
return $found_label[$theKey];
I thought they should be a better way of doing this, also I'm new to PHP.
You can't do that with array_filter, there is no way to stop it and return only the first result. If you can't use the key to extract the result you want from the array returned from array_filter, you should use a loop. Something like this:
$label = "wantedLabel";
foreach ($labels as $l) {
if( $l->name === $label ) {
print_r ($l);
break;
}
}
This:
<?php
$labels = array(
"0" => (object) array('name' => "name1", "title" => "title1"),
"1" => (object) array('name' => "name2", "title" => "title2")
);
$label = "name1";
$found_label = array_filter($labels, function($obj) use($label) {
return $obj->name === $label;
});
print_r($found_label[0]);
Produces:
stdClass Object ( [name] => name1 [title] => title1 )

Group same array values inside object into same keys

I have the array below:
[1]=>
array(2) {
[0]=>
object(stdClass)#23 (7) {
["AddressesTableID"]=> string(1) "8"
["AccreditNo"]=> string(13) "5129876de28ff"
["Type"]=> string(4) "home"
["Street"]=> string(34) "Wallace, Village, Under the bridge"
["Municipality"]=> string(8) "Tortuous"
["Province"]=> string(8) "Sardonic"
["ContactNo"]=> string(8) "92012010"
}
[1]=>
object(stdClass)#24 (7) {
["AddressesTableID"]=> string(1) "9"
["AccreditNo"]=> string(13) "5129876de28ff"
["Type"]=> string(6) "office"
["Street"]=> string(25) "Rasputin Query, Palpitate"
["Municipality"]=> string(7) "Opulent"
["Province"]=> string(6) "Number"
["ContactNo"]=> string(8) "29101011"
}
}
Can you guys help me on how to transform this into:
Where all the values are grouped as an array into similar keys?
["AccreditNo"]=> array(2) { "5129876de28ff", "GKIJUGUIKGI" }
["Type"]=> array(2) { "home", "home" }
["Street"]=> ...
["Municipality"]=> ...
["Province"]=> ...
["ContactNo"]=> ...
Try with:
$input = array( /* your input data*/ );
$output = array();
foreach ( $input as $data ) {
foreach ( $data as $key => $value ) {
if ( !isset($output[$key]) ) {
$output[$key] = array();
}
$output[$key][] = $value;
}
}
You can use the array_merge_recursive() function, as follows:
$arr = array_merge_recursive($arr1, $arr2)
for more information, please see here:http://www.php.net/manual/en/function.array-merge-recursive.php

Categories