Creating a custom multidimensional array - php

I have been stumped on this for awhile and was wondering if there was a way to turn this data into a custom multidimensional array.
I run a query on my table and it spits out an array like this
SELECT id_cms_category, name FROM cms_category_lang ORDER BY id_cms_category ASC
The resulted array looks like this.
array
0=>
array
'id_cms_category'=> 1
'name'=>'Home'
1=>
array
'id_cms_category'=> 2
'name'=>'Test'
So then I run my query on my cms table
SELECT cl.id_cms, c.id_cms_category, cl.meta_title
FROM cms_lang cl
LEFT JOIN cms c ON (c.id_cms = cl.id_cms)
LEFT JOIN cms_link ci ON (ci.id_cms = cl.id_cms)
ORDER BY c.id_cms_category, cl.meta_title ASC
and I get an array like this
array
0 =>
array
'id_cms' => string '4' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'About us' (length=8)
1 =>
array
'id_cms' => string '6' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Contact Us' (length=10)
2 =>
array
'id_cms' => string '1' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Delivery' (length=8)
3 =>
array
'id_cms' => string '2' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Legal Notice' (length=12)
4 =>
array
'id_cms' => string '5' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Secure payment' (length=14)
5 =>
array
'id_cms' => string '3' (length=1)
'id_cms_category' => string '1' (length=1)
'meta_title' => string 'Terms and conditions of use' (length=27)
6 =>
array
'id_cms' => string '10' (length=2)
'id_cms_category' => string '2' (length=1)
'meta_title' => string 'FAQ - UltraTech IBC Spill Pallet Plus' (length=37)
7 =>
array
'id_cms' => string '9' (length=1)
'id_cms_category' => string '2' (length=1)
'meta_title' => string 'FAQ - UltraTech P2 Plus 2-Drum Hard Top Spill Pallet' (length=52)
8 =>
array
'id_cms' => string '7' (length=1)
'id_cms_category' => string '2' (length=1)
'meta_title' => string 'XR-5, Urethane & Copolymer 2000' (length=31)
I am wanting to try and take all this data and somehow create a multidimensional array like so;
array
0 => 'Home'
array
id_cms => meta_title
1 => 'Test'
array
id_cms => meta_title
that way I should be able to run something like this
foreach($title as $key => $value)
{
<li>$value</li>
foreach($value as $id_cms => $page)
{
<li>$page</li>
}
}
to get the output on the page to look something like this
Home
home 1
home 2
home 3
Test
test 1
test 2
test 3

I wouldn't do two queries for this.
I would alter your second query so you've got both the category and page name in the same result set. It should look something like:
SELECT cl.id_cms, c.id_cms_category, catlang.name, cl.meta_title
FROM cms_lang cl
LEFT JOIN cms c
ON (c.id_cms = cl.id_cms)
LEFT JOIN cms_link ci
ON (ci.id_cms = cl.id_cms)
LEFT JOIN cms_category_lang catlang
ON (catlang.id_cms_category = cl.id_cms_category)
ORDER BY c.id_cms_category, catlang.name, cl.meta_title ASC;
Then you could create a loop that simply checked the category in each iteration. If it finds a new one, end the previous block and start a new one:
$currentCategory = '';
$output = '';
foreach ($rows as $row) {
if ($currentCategory != $row['name']) {
$output .= '</li><li>' . $row['name'];
}
$currentCategory = $row['name'];
$output .= '<li><a href="' . $row['id_cms'] . '">' . $row['meta_title'] . '</li>';
}
// Trim off our unnecessary </li>
$output = substr($output, 4);

If your CMS returns a numerically indexed array (and that's what it sounds like from your post), you can re-index the array using whatever key you want.
$array = array(0 => array('id_cms' => 4, 'id_cms_category' => 1, 'meta_title' => 'About us'));
$newArray = array();
foreach ( $array as $v ) {
$newArray[$v['meta_title']] = $v;
}
var_dump($newArray);
Arrays are vital in PHP programming, so you'd be doing yourself a favor to learn them before going any further.

Create a new empty array: $newArray = array();
Go through your result with a foreach, then as you go, check if the category of the current item already exists in your new array: if (isset($newArray[$row['id_cms_category']))
If it doesn't, create it and initialize it with a new array containing the item as a first element: $newArray[$row['id_cms_category'] = array($row);
If it already existed (i.e. you already went through an item belonging to this category), then add it: $newArray[$row['id_cms_category'] []= $row;
That's it!

Related

Store unique values within foreach loop

Im trying to get value of ids for different values of a foreach loop but im confused how to go about it.
$revenue = array('pseudo1', 'pseudo2');
foreach ($revenue as $value) {
if (!$revenue_type) {
$st = getValueDescription($value);
foreach ($st as $stype) {
$tpe[] = $stype->id;
}
$rev[$value] = $tpe;
}
}
when i dump $rev this is what i get
array (size=1)
0 =>
array (size=3)
'pseudo1' =>
array (size=2)
0 => string '9' (length=1)
1 => string '19' (length=2)
'pseudo2' =>
array (size=4)
0 => string '9' (length=1)
1 => string '19' (length=2)
2 => string '1' (length=1)
3 => string '35' (length=2)
what i actually expect
array (size=1)
0 =>
array (size=3)
'pseudo1' =>
array (size=2)
0 => string '9' (length=1)
1 => string '19' (length=2)
'pseudo2' =>
array (size=4)
2 => string '1' (length=1)
3 => string '35' (length=2)
I need the result of my $rev to contain $value as keys but previous values of $tpe keeps adding up with each iteration, im confused how to achieve this.
Does this solve your problem?
$revenue = array('pseudo1', 'pseudo2');
$uniqueValues = [];
foreach ($revenue as $value) {
if (!$revenue_type) {
$st = getValueDescription($value);
$tpe = [];
foreach ($st as $stype) {
if (!in_array($stype->id, $uniqueValues)) {
$tpe[] = $stype->id;
$uniqueValues[] = $stype->id;
}
}
$rev[$value] = $tpe;
}
}
$uniqueValues holds the IDS you already added to the $rev variable, and the $tpe gets empty after every iteration

How do i swap values?

I have list of departments, against each department there is a button which is UP and another button DOWN
When you click on UP Button, It calls the function up..
Public function actionUP($id, $sort_order){
$allDeps = Department::model()->findAll($criteria);
$depArr = array();
foreach ($allDeps as $department){
$temp = array();
$temp['id'] = $department->id;
$temp['sort_order'] = $department->sort_order;
$depArr[] = $temp;
}
Now if i var dump this array depArr i am getting an array..
array (size=17)
0 =>
array (size=2)
'id' => string '142' (length=3)
'sort_order' => string '1' (length=1)
1 =>
array (size=2)
'id' => string '141' (length=3)
'sort_order' => string '2' (length=1)
2 =>
array (size=2)
'id' => string '144' (length=3)
'sort_order' => string '5' (length=1)
3 =>
array (size=2)
'id' => string '140' (length=3)
'sort_order' => string '6' (length=1)
4 =>
array (size=2)
'id' => string '139' (length=3)
'sort_order' => string '3' (length=1)
5 =>
array (size=2)
'id' => string '143' (length=3)
'sort_order' => string '4' (length=1)
I have list of departments, position is actually sort_order and there are up and down button against department, up buttons calls for actionUP which sends the up?id=143&sort_order=4
If a user clicks on up button against position 4 its position should changed to 3, and the department at 3 should go to 4 and ALSO UPDATE INTO DATABASE..
I am using Yii 1.1 PHP framework
Flow:
Search using
array_search('whateverID', array_column($depArr, 'id'));
Now you have the index and the sort order, you just need to search for sort order - 1 now.
array_search($depArr[x]['sort_order']-1, array_column($depArr,
'sort_order'));
You now have both element's index, swap the sort order or unset and set again. Hope this helps.

Php Convert Multidimensional array to string

Below is my function:
public function getChildrenId(){
$child_id = array($this->db->query("SELECT customer_id
FROM " . DB_PREFIX . "customer
WHERE parent IN ( " .(int)$this->customer->getId().") "));
foreach($child_id as $id =>$value) {
$conv = json_decode(json_encode($value), true);
$final = array_slice($conv,2);
foreach ($final as $gchildren => $key) {
sort($key);
$gr = array_slice($key,0,$this->INF);
}
}
return $gr;
}
It outputs:
array (size=3)
0 =>
array (size=1)
'customer_id' => string '2' (length=1)
1 =>
array (size=1)
'customer_id' => string '4' (length=1)
2 =>
array (size=1)
'customer_id' => string '7' (length=1)
I am trying to get the values of the nested arrays. When I use foreach I only get data from array[0]. I also tried slicing the parent array and still didn't get it right, it outputs array,array,array.
I would like to extract these arrays values to a new array that I can use to query the database. final_array = array (2,4,7).
Thank you in advance!
If your array looks like this, then the foreach should create the array your looking for.
array (size=3)
0 =>
array (size=1)
'customer_id' => string '2' (length=1)
1 =>
array (size=1)
'customer_id' => string '4' (length=1)
2 =>
array (size=1)
'customer_id' => string '7' (length=1)
The following php will output array(2,4,7);
<?php
$aNewArray = array();
foreach($aArray as $aArray){
$aNewArray[] = $aArray['customer_id'];
}
var_dump($aNewArray);
?>
You dont need a multidimensional array for this though.

How to display array value separately with selected column?

I want to display array 1 by 1.. 1st array value display thnn 2nd array value
I want to display array data using loop and display data separatelylike 1st array data display then 2nd array data will display. and i want to display data only entrydate n description. with loop count
$svs= $user_posts->getvalue('service', $dsplylimit );
var_dump($svs);
My array is like this
array (size=2)
0 =>
array (size=46)
0 => string '74' (length=2)
'id' => string '74' (length=2)
1 => string '0' (length=1)
'author' => string '0' (length=1)
2 => string '2016-02-08 07:32:08' (length=19)
'entrydate' => string '2016-02-08 07:32:08' (length=19)
3 => string '2016-02-08 01:32:08' (length=19)
'date_gmt' => string '2016-02-08 01:32:08' (length=19)
4 => string 'nice email' (length=10)
'description' => string 'nice email' (length=10)
1 =>
array (size=46)
0 => string '75' (length=2)
'id' => string '75' (length=2)
1 => string '0' (length=1)
'author' => string '0' (length=1)
2 => string '2016-02-08 11:15:40' (length=19)
'entrydate' => string '2016-02-08 11:15:40' (length=19)
3 => string '2016-02-08 05:15:40' (length=19)
'date_gmt' => string '2016-02-08 05:15:40' (length=19)
4 => string 'hiiiiiii' (length=8)
'description' => string 'hiiiiiii' (length=8)
It isn't very clear what you want to do, but you can probably display the data with a foreach loop. Just loop through each array, and then print out the values from each iteration that you want.
For example for the following array
$data = [
['entrydate' => '2016-01-01', 'description' => 'Some description here'],
['entrydate' => '2016-01-02', 'description' => 'Another description here']
];
You could print out the data like so:
$html = '';
foreach ($data as $array) {
$html .= '<h1>' . htmlspecialchars($array['entrydate']) . '</h1>';
$html .= '<p>' . htmlspecialchars($array['description']) . '</p>';
}
echo $html;
For the following output:
2016-01-01
Some description here
2016-01-02
Another description here
There's a chance I'm not understanding your requirements, if so just let me know, but I'm not 100% sure what it is you're after.

Split Array at Repeating Object in Array

I am parsing an XML file and creating two arrays: one of the XML tags ($tags), and the other as the values for the tags ($values). As it parses, it adds the tags and values as it goes, when it's done, I implode the arrays and put them into a MySQL statement:
$sql = "INSERT INTO everything ($tags) VALUE ($values)";
This works fine until I have repeating tags, and then the SQL statement doesn't work....
Is there a way to find the first repeated word in the $tags array and split it at that word (Keeping the tags that follow it) and also split the $values array at the same index that $tags was split, so that the information stays in the same order?
So ultimately converting something like this:
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUE ('1','1','D','A','2015','64','OX','1','18','-2','64','WA','1','23','-2','29','WAG','1','49','77','97');
Into something like:
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES ('1','1','D','A','2015','64','OX','1','18','-2');
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES ('64','WA','1','23','-2');
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUES ('29','WAG','1','49','77','97');
Thanks in advance!...
I just base from your "something like".. :)
$fields = ['AmazonOrderID', 'MerchantOrderID', 'ShipmentID', 'MerchantFulfillmentID', 'PostedDate', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'AmazonOrderItemCode', 'SKU', 'Quantity', 'Principal', 'Commission', 'FBA'];
$values = ['1','1','D','A','2015','64','OX','1','18','-2','64','WA','1','23','-2','29','WAG','1','49','77','97'];
// i just added this to avoid error produced by: `Undefined offset` error warning
error_reporting(0);
$fields_dup = array();
$values_dup = array();
for ($i = 0, $j = 0; $i < count($fields); $i++)
{
if (in_array($fields[$i], $fields_dup[$j]))
$j++;
$fields_dup[$j][] = $fields[$i];
$values_dup[$j][] = $values[$i];
// or maybe you want to add ` and ' make your statement look like:
// INSERT INTO table (`field1`, `field2`) VALUES ('value1', 'value2')
//
// $fields_dup[$j][] = "`".$fields[$i]."`";
// $values_dup[$j][] = "'".$values[$i]."'";
}
error_reporting(E_ALL);
// just to show what is produced
var_dump($fields_dup);
var_dump($values_dup);
// while you can also construct your statement in a loop like
for ($i = 0; $i < count($fields_dup); $i++)
{
$sql_fields = implode(',', $fields_dup[$i]);
$sql_values = implode(',', $values_dup[$i]);
echo "INSERT INTO everything ($sql_fields) VALUES ($sql_values) <br>";
}
Output would be:
//var_dump($fields_dup);
array (size=3)
0 =>
array (size=10)
0 => string 'AmazonOrderID' (length=13)
1 => string 'MerchantOrderID' (length=15)
2 => string 'ShipmentID' (length=10)
3 => string 'MerchantFulfillmentID' (length=21)
4 => string 'PostedDate' (length=10)
5 => string 'AmazonOrderItemCode' (length=19)
6 => string 'SKU' (length=3)
7 => string 'Quantity' (length=8)
8 => string 'Principal' (length=9)
9 => string 'Commission' (length=10)
1 =>
array (size=5)
0 => string 'AmazonOrderItemCode' (length=19)
1 => string 'SKU' (length=3)
2 => string 'Quantity' (length=8)
3 => string 'Principal' (length=9)
4 => string 'Commission' (length=10)
2 =>
array (size=6)
0 => string 'AmazonOrderItemCode' (length=19)
1 => string 'SKU' (length=3)
2 => string 'Quantity' (length=8)
3 => string 'Principal' (length=9)
4 => string 'Commission' (length=10)
5 => string 'FBA' (length=3)
// var_dump($values_dup);
array (size=3)
0 =>
array (size=10)
0 => string '1' (length=1)
1 => string '1' (length=1)
2 => string 'D' (length=1)
3 => string 'A' (length=1)
4 => string '2015' (length=4)
5 => string '64' (length=2)
6 => string 'OX' (length=2)
7 => string '1' (length=1)
8 => string '18' (length=2)
9 => string '-2' (length=2)
1 =>
array (size=5)
0 => string '64' (length=2)
1 => string 'WA' (length=2)
2 => string '1' (length=1)
3 => string '23' (length=2)
4 => string '-2' (length=2)
2 =>
array (size=6)
0 => string '29' (length=2)
1 => string 'WAG' (length=3)
2 => string '1' (length=1)
3 => string '49' (length=2)
4 => string '77' (length=2)
5 => string '97' (length=2)
// for the last for-statement
INSERT INTO everything (AmazonOrderID,MerchantOrderID,ShipmentID,MerchantFulfillmentID,PostedDate,AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES (1,1,D,A,2015,64,OX,1,18,-2)
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission) VALUES (64,WA,1,23,-2)
INSERT INTO everything (AmazonOrderItemCode,SKU,Quantity,Principal,Commission,FBA) VALUES (29,WAG,1,49,77,97)
Is that what you are trying to do?
Hope this is helpful, Cheers! ;)

Categories