Unset is not unsetting Array values - php

I am trying to build a dynamic Menu from data I generate from jstree.
Jstree values will stored in database as follows:
Id | Name | Parent | Icon | Link
So I've made a method to read each row from database(propel) and store it in array, so Smarty can build a menu dynamically.
That's my function:
$menu_content = array();
foreach($result as $key => $r) {
if($r['Id'] != 1) {
$Id = $r['Id'];
$menu_content[$r['Id']] = array(
"name" => $r['Name'],
"link" => $r['Link'],
"icon" => $r['Icon']
);
unset($result[$key]);
foreach($result as $key_children => $c) {
if($c['Parent'] == $r['Id']) {
$menu_content[$r['Id']]['children'][] = array(
"name" => $c['Name'],
"link" => $c['Link'],
"icon" => $c['Icon']
);
unset($result[$key_children]);
$Idc = $c['Id'];
foreach($result as $key_grandchild => $cc) {
if($cc['Parent'] == $c['Id']) {
$menu_content[$r['Id']]['children']['grandchild'][] = array(
"name" => $cc['Name'],
"link" => $cc['Link'],
"icon" => $cc['Icon']
);
unset($result[$key_grandchild]);
}
}
}
}
}
So it should store values like this:
$menu_content[parent][children][grandchildren].
This part of code is working fine, but it's not unsetting the children and grandchildren values, so I get them twiche. First time in correct order and after that as parent children.
Thanks in advance.

Related

3d nested array foreach statement issue

I have this array written below, and I know it isnt pretty, sorry. I come to this array structure as it is the only way I could think of when dealing with my post request.
$_POST = array("person" => array(
[1] => array("id" => 1, "name" => "bob"),
[2] => array("id" => 2, "name" => "jim")
)
);
I want to be able to pick "name" from certain "id", so below code is what I came up with. In the example below, if person["id"] is equal to 1, retrieve its "name" which is "bob".
foreach ($_POST as $dataSet) {
foreach ($dataSet as $person) {
foreach ($person as $field => $value) {
if ($person["id"] == 1) {
echo $person["name"];
}
}
}
}
The problem I am having is as I execute the code.
the result is bobbob,
it seems like the code looped the if statement twice (same as the number of elements in the person array). I know if I put break into the code, then it will solve it, but anyone know why it looped twice? Maybe this will deepen my foreach and array understanding.
There is no need to have third nested loop. Hope this one will be helpful.
Problem: In the third loop you were iterating over Persons: array("id" => 1, "name" => "bob") which have two keys. and you are checking only single static key $person["id"], that's why it was printing twice.
Solution 1:
Try this code snippet here
<?php
ini_set('display_errors', 1);
$POSTData = array("person" => array(
1 => array("id" => 1, "name" => "bob"),
2 => array("id" => 2, "name" => "jim")
)
);
foreach ($POSTData as $dataSet)
{
foreach ($dataSet as $person)
{
if ($person["id"] == 1)
{
echo $person["name"];
}
}
}
Solution 2:
Alternatively you can try this single line solution.
Try this code snippet here
echo array_column($POSTData["person"],"name","id")[1];//here 1 is the `id` you want.
You must have seen the other answers, and they have already said that you dont need the 3rd loop. but still if you want to keep the third loop.
you can use this code.
foreach ($_POST as $dataSet) {
foreach ($dataSet as $person) {
foreach ($person as $field => $value) {
if($value == 1){
echo $person['name'];
}
}
}
}
No need of third foreach
<?php
$mainArr = array("person" => array(
1 => array("id" => 1, "name" => "bob"),
2 => array("id" => 2, "name" => "jim")
)
);
foreach ($mainArr as $dataSet) {
foreach ($dataSet as $person) {
if ($person["id"] == 1) {
echo $person["name"];
break;
}
}
}
?>
Live demo : https://eval.in/855386
Although it's unclear why you need to do a POST in this fashion, here's how to get "bob" only once:
<?php
$_POST = array("person" => array(
1 => array("id" => 1, "name" => "bob"),
2 => array("id" => 2, "name" => "jim")
)
);
$arr = array_pop($_POST);
foreach($arr as $a) {
if ($a["id"] == 1) {
echo $a["name"];
}
}
Array_pop() is useful for removing the first element of the array whose value is an array itself which looks like this:
array(2) {
[1]=>
array(2) {
["id"]=>
int(1)
["name"]=>
string(3) "bob"
}
[2]=>
array(2) {
["id"]=>
int(2)
["name"]=>
string(3) "jim"
}
}
When the if conditional evaluates as true which occurs only once then the name "bob" displays.
See live code.
Alternatively, you could use a couple of loops as follows:
foreach ($_POST["person"] as $data) {
foreach ($data as $value) {
if ( $value == 1) {
echo $data["name"],"\n";
}
}
}
See demo
As you mentioned, I want to be able to pick name from certain id, : No need of nested looping for that. You can do like this using array_column and array_search :
$data = array("person" => array(
1 => array("id" => 1, "name" => "bob"),
2 => array("id" => 2, "name" => "jim")
)
);
// 1 is id you want to search for
$key = array_search(1, array_column($data['person'], 'id'));
echo $data['person'][$key + 1]['name']; // $key + 1 as you have started array with 1
Output:
bob
with foreach:
foreach ($data as $dataValue) {
foreach ($dataValue as $person) {
if ($person['id'] === 1) {
echo $person["name"];
}
}
}

How to display the value of array as string or text not as index value

I have two table one for sections where each section has many questions when i echo i got the section text but the questions as index value 0 1 2
<?php
$result = mysql_query($query);
if ($result) {
$data = array();
while ($row = mysql_fetch_assoc($result)) {
$data[($row['SECTION_NAME'])][][($row['QUES_TEXT'])][] = array(
'SECTION' => $row['SECTION_NAME'],
'QUESTION' => $row['QUES_TEXT']
);
}
foreach ($data as $SECTION => $QUESTIONS) {
echo '<h2>',htmlentities($SECTION),'</h2>';
foreach ($QUESTIONS as $QUESTIONS_TEXT => $TEXT) {
echo '<h2>',($QUESTIONS_TEXT),'</h2>';
}
}
}
?>
You are creating extra nested arrays and also the parens are unneeded:
$data[$row['SECTION_NAME']][][$row['QUES_TEXT']][] = array(
'SECTION' => $row['SECTION_NAME'],
'QUESTION' => $row['QUES_TEXT']
);
Should be:
if (empty($data[$row['SECTION_NAME']]) {
$data[$row['SECTION_NAME']] = array();
}
$data[$row['SECTION_NAME']][$row['QUES_TEXT']] = array(
'SECTION' => $row['SECTION_NAME'],
'QUESTION' => $row['QUES_TEXT']
);
Also please see the comments to your original question, this is no longer a safe way to use MySQL from PHP.

Get count of each element returned by mysql query

This is my query:
SELECT `Brand`,`Colour`,`Occassion`,`Fabric`,`Type`
FROM `deals`
WHERE `Size`
LIKE '%XS%';
It returns 35 results. Now, what i want is a count of each of the columns (Brand, colour etc) which are present in the above resultset to create a histogram. I am not sure about how to do this.
Any help is appreciated.
I think ideal result should look like this:
$data = array(
"Brand" => array(brand1 => 1, brand2 => 2),
"Colour" => array(colour1 => 1, colour2 => 2),
"Occassion" => array(Occassion1 => 1, Occassion2 => 2),
);
For each subarray we can draw a histogram. The code will look like this:
$query = "
SELECT
`Brand`,`Colour`,`Occassion`,`Fabric`,`Type`
FROM
`deals`
WHERE
`Size` LIKE '%XS%'";
$data = array(
"Brand" => array(),
"Colour" => array(),
"Occassion" => array(),
"Fabric" => array(),
"Type" => array(),
);
if ($result = $mysqli->query($query)) {
/* fetch associative array */
while ($row = $result->fetch_assoc()) {
foreach($row as $key => $value)
{
$data[$key][$value]++;
}
}
/* free result set */
$result->free();
}
Or we can define $data subarrays inside foreach to make the program more flexible:
$data = array();
...
foreach($row as $key => $value)
{
if(!isset($data[$key]))
{
$data[$key] = array();
}
$data[$key][$value]++;
}
...

Function only returns first array

I'm trying to add some settings to my WordPress options page that depend on the number of categories. I created this function to use inside the main array, but it only returns the first array, leaving out the other 3 I have. A print_r will show all of them, so I can't seem to figure this out.
function listSections() {
$categories = get_categories();
foreach($categories as $category) {
return array (
"name" => $category->cat_name . " Label Color",
"desc" => "Select a label color.",
"id" => $shortname."_label_color" . $category->cat_ID,
"type" => "select",
"options" => $color_options,
"std" => ""
);
}
}
You can only return once!
function listSections() {
$categories = get_categories();
$return = array();
foreach($categories as $category) {
$return[] = array (
"name" => $category->cat_name . " Label Color",
"desc" => "Select a label color.",
"id" => $shortname."_label_color" . $category->cat_ID,
"type" => "select",
"options" => $color_options,
"std" => ""
);
}
return $return;
}
The fix is push each array into a temporary array, then return that array at the end of the loop.
The function can only return once. It cannot return multiple things in a loop. After it reaches the first return, it exits the function completely. If you want to return an array of arrays, you should use the following.
function listSections() {
$results = array();
$categories = get_categories();
foreach($categories as $category) {
$results[] = array (
"name" => $category->cat_name . " Label Color",
"desc" => "Select a label color.",
"id" => $shortname."_label_color" . $category->cat_ID,
"type" => "select",
"options" => $color_options,
"std" => ""
);
}
return $results;
}
using the syntax $result[] = xyz; will append xyz to the end of the array. You can loop through the returned array, with some code like
$results = listSections();
$count = count($results);
for ($i = 0; $i < $count; $i++) {
$category = $results[$i];
echo $category["name"];
etc......
}
When you call return from a function it always immediately ends the execution of that function, so as soon as the first array gets returned, the function ends - which is why you are only getting the first array back.
You could try returning a multi-dimensional array (an array that contains all of the arrays you'd like to be returned) instead.
The goal of the returnkeyword is to exit the function. So it is normal that your fonction only return the first element.
You can for exemple put all the elements into an array and return this array :
function listSections() {
$categories = get_categories();
$arr = array();
foreach($categories as $category) {
$arr[] = array (
"name" => $category->cat_name . " Label Color",
"desc" => "Select a label color.",
"id" => $shortname."_label_color" . $category->cat_ID,
"type" => "select",
"options" => $color_options,
"std" => ""
);
}
return $arr;
}

PHP: How to create a jagged array structure to represent grouped records from a database

This seems like a simple challenge, but I'm struggling.
I want to retrieve records using a join query on two database tables and represent them as an array of arrays, whereby each of the elements in the root array is a parent record and each nested element represents a child record.
The SQL query is working fine, and it returns a set of rows in which the channel_key column is a grouping column.
Here's my attempt at populating the array structure from the rows:
$rows = $db->get_results($query);
$key = '';
$programmes = array();
foreach ($rows as $row) {
$programme = array(
'title' => $row->title,
'start' => $row->start,
'duration' => $row->duration
);
$programmes[] = $programme;
if ($key != $row->channel_key) {
$channels[] = array(
'key' => $row->channel_key,
'programme' => $programmes
);
$key = $row->channel_key;
$programmes = array();
}
}
Unfortunately this only populates the root level arrays (the ones that correspond to the parent records).
Any suggestions please?
Thanks,
Tim
You only have one programme in the $programmes array when you assign it to the channel.
An alternative would be to build the channel array with the channel_key.
e.g.
<?php
$channels = array();
foreach($rows as $row) {
// Create the channel node if it doesn't exist
if (!isset($channels[$row->channel_key])) {
$channels[$row->channel_key] = array(
'key' => $row->channel_key,
'programme' => array()
);
}
$programme = array(
'title' => $row->title,
'start' => $row->start,
'duration' => $row->duration
);
// Add to the existing channel node.
$channels[$row->channel_key]['programme'][] = $programme;
}
Simple solution can be.
$rows = $db->get_results($query);
$key = '';
$programmes = array();
foreach ($rows as $row) {
$programme = array(
'title' => $row->title,
'start' => $row->start,
'duration' => $row->duration
);
$programmes[$row->channel_key][] = $programme;
}

Categories