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]);
}
Related
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
);
}
I have an array with a lot of nested array data.
I need to get:
array(4) { ["id"]=> int(90)
this number, the 90.
Please see the whole array data below.
Thank you very much for help.
array(2)
{ ["_sft_product_cat"]=>array(5)
{
["name"]=> string(18)
"Produkt-Kategorien"
["singular_name"]=> string(9)
"Kategorie"
["all_items_label"]=> string(15)
"Alle Kategorien"
["type"]=> string(8)
"taxonomy"
["active_terms"]=> array(1)
{ [0]=> array(4)
{ ["id"]
=> int(90) ["name"]
=> string(7) "Hyundai" ["value"]
=> string(7) "hyundai" ["count"]
=> int(0)
}
}
}
["_sft_pa_typ"]=> array(5)
{ ["name"]
=> string(18) "Produkt Typ/Modell" ["singular_name"]
=> string(10) "Typ/Modell" ["all_items_label"]
=> string(5) "Al le " ["type"]
=> string(8) "taxonomy" ["active_terms"]
=> array(1)
{ [0]=> array(4)
{ ["id"]
=> int(9040) ["name"]
=> string(8) "Coupe GK" ["value"]
=> string(8) "coupe-gk" ["count"]=> int(0) } } } }
You're looking for:
$whatever_your_var_name_is["_sft_product_cat"]["active_terms"][0]["id"]
That is the same as referencing the levels one by one...
$sft_product_cat = $whatever_your_var_name_is['_sft_product_cat'];
$active_terms = $sft_product_cat['active_terms'];
$first_terms = $active_terms[0];
$id = $first_terms['id'];
It's notable that active_terms has a numeric indexed array (the [0] part). It's a hint that other instances of '_sft_product_cat' might have more than 1 terms and you'll need a loop to get them all.
I'm not familiar with php, but firstly I would format and indent the code to get a better picture of what you need from the nested objects:
array(2) {
["_sft_product_cat"]=> array(5)
{
["name"]=> string(18) "Produkt-Kategorien"
["singular_name"]=> string(9) "Kategorie"
["all_items_label"]=> string(15) "Alle Kategorien"
["type"]=> string(8) "taxonomy"
["active_terms"]=> array(1)
{
[0]=> array(4)
{
["id"]=> int(90)
["name"]=> string(7) "Hyundai"
["value"]=> string(7) "hyundai"
["count"]=> int(0)
}
}
}
["_sft_pa_typ"]=> array(5)
{
["name"]=> string(18) "Produkt Typ/Modell"
["singular_name"]=> string(10) "Typ/Modell"
["all_items_label"]=> string(5) "Alle "
["type"]=> string(8) "taxonomy"
["active_terms"]=> array(1)
{
[0]=> array(4)
{
["id"]=> int(9040) ["name"]=> string(8) "Coupe GK"
["value"]=> string(8) "coupe-gk" ["count"]=> int(0)
}
}
}
}
This would lead me to believe that you're after the id at:
<object>["_sft_product_cat"]["active_terms"][0]["id]
or
<object>[0][4][0][0]
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
I'm looking for a solution to create an associative array from an flat array data in foreach loop:
What i have is a csv/xls file that has header on first row and data in next rows.
Row1: header
Row2,3,4,5: data
the array looks:
array(3) {
[0]=>
array(7) {
[0]=>
string(3) "country"
[1]=>
string(7) "state"
[2]=>
string(3) "city"
[3]=>
string(5) "name"
[4]=>
string(4) "address"
[5]=>
string(6) "gender"
[6]=>
string(6) "status"
}
[1]=>
array(7) {
[0]=>
string(12) "Argentina"
[1]=>
string(12) "Corrientes"
[2]=>
string(12) "Corrientes"
[3]=>
string(12) "Jorge"
[4]=>
string(12) "Avenida Avellaneda 12"
[5]=>
string(12) "Masculino"
[6]=>
string(12) "Activo"
}
[2]=>
array(7) {
[0]=>
string(12) "Argentina"
[1]=>
string(12) "Chaco"
[2]=>
string(12) "Resistencia"
[3]=>
string(12) "Mariano"
[4]=>
string(12) "Avenida Peron 12"
[5]=>
string(12) "Masculino"
[6]=>
string(12) "Activo"
}
}
The result i need to get at the end is:
array(2) {
[0]=>
array(7) {
['country']=>
string(12) "Argentina"
['state']=>
string(12) "Corrientes"
['city']=>
string(12) "Corrientes"
['name']=>
string(12) "Jorge"
['address']=>
string(12) "Avenida Avellaneda 12"
['gender']=>
string(12) "Masculino"
['status']=>
string(12) "Activo"
}
[1]=>
array(7) {
['country']=>
string(12) "Argentina"
['state']=>
string(12) "Chaco"
['city']=>
string(12) "Resistencia"
['name']=>
string(12) "Mariano"
['address']=>
string(12) "Avenida Peron 12"
['gender']=>
string(12) "Masculino"
['status']=>
string(12) "Activo"
}
}
$array = $your_flat_array;
for ($i = 1; $i < count($array); $i++) {
$new_array[$i-1] = [];
foreach ($array[$i] as $key => $value) {
$new_array[$i-1][$array[0][$key]] = $value;
}
}
print_r($new_array);
create a multidimensional array from an flat array
You already have a multidimensional array, because you got arrays in an array.
What you can do in this specific case is to use array_splice() in combination with array_combine().
Try this:
$oldArray = array(
array( "country", "state", "city", "name" ),
array( "Argentina", "Corrientes", "Corrientes", "Jorge" ),
array( "Argentina", "Chaco", "Resistencia", "Mariano" )
);
$newArray = array_splice( $oldArray, 1 );
foreach( $newArray as $index => $array ) {
$newArray[$index] = array_combine( $oldArray[0], $array );
}
echo "<pre>";
var_dump( $newArray );
OUTPUT:
array(2) {
[0]=>
array(4) {
["country"]=>
string(9) "Argentina"
["state"]=>
string(10) "Corrientes"
["city"]=>
string(10) "Corrientes"
["name"]=>
string(5) "Jorge"
}
[1]=>
array(4) {
["country"]=>
string(9) "Argentina"
["state"]=>
string(5) "Chaco"
["city"]=>
string(11) "Resistencia"
["name"]=>
string(7) "Mariano"
}
}
All you have to do is remove the first item (header row) from your array:
array_splice($yourArray, 0, 1);
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));