Search array and delete entries - php

How do a delete all array entries where status equals 0?
That means: $array[1][3]; and $array[4];
Array
(
[1] => Array
(
[id] => 1
[parent_id] => 0
[status] => 2
[title] => bananer
[breadcrumb] => /bananer
[slug] => /bananer
[2] => Array
(
[id] => 2
[parent_id] => 1
[status] => 2
[title] => sub bananer
[breadcrumb] => /bananer/sub bananer
[slug] => /bananer/sub-bananer
)
[3] => Array
(
[id] => 3
[parent_id] => 1
[status] => 0
[title] => sub bananer 2
[breadcrumb] => /bananer/sub bananer 2
[slug] => /bananer/sub-bananer-2
)
)
[4] => Array
(
[id] => 4
[parent_id] => 0
[status] => 0
[title] => appelsin
[breadcrumb] => /appelsin
[slug] => /appelsin
[5] => Array
(
[id] => 5
[parent_id] => 4
[status] => 2
[title] => sub appelsin
[breadcrumb] => /appelsin/sub appelsin
[slug] => /appelsin/sub-appelsin
)
[6] => Array
(
[id] => 6
[parent_id] => 4
[status] => 2
[title] => sub appelsin 2
[breadcrumb] => /appelsin/sub appelsin 2
[slug] => /appelsin/sub-appelsin-2
)
)
)

Try this:
function deleteIt(&$array)
{
foreach($array as $key => &$value)
{
if (is_numeric($key)) deleteIt($value);
if (isset($value['status']) && !$value['status']) unset($array[$key]);
}
}
This runs through the array recursively while handing references down so that the original array is modified while deleting. Notice the & in the deleteIt function prototype and in the foreach.

Something like this may help
function parse($var)
{
foreach($var as $key => $val)
{
if( is_array($val) )
{
parse($val)
}
else
{
if($key == 'status' && $val ==0 )
{
// do something here
}
}
}
}

You can do something that's highly specialized such as:
function stripBadStatus($array) {
foreach($array as $k=>$arr) {
if($arr['status'] == 0) {
unset($array[$k]);
} else if(is_array($arr)) {
foreach($arr as $deepk=>$deeparr) {
if($deeparr['status'] == 0) {
unset($array[$k][$deepk]);
}
}
}
}
return $array;
}

Related

Sort all child by parent id

i have array like this:
(
[0] => Array
(
[id] => 1
[name] => Bazowa
[parent_id] => 0
)
[1] => Array
(
[id] => 2
[name] => Główna
[parent_id] => 1
)
[2] => Array
(
[id] => 12
[name] => PlayStation
[parent_id] => 2
)
[3] => Array
(
[id] => 13
[name] => Xbox
[parent_id] => 2
)
[4] => Array
(
[id] => 14
[name] => Nintendo
[parent_id] => 2
)
[5] => Array
(
[id] => 15
[name] => PC
[parent_id] => 2
)
)
and i want sort this array like tree on screenshot below:
Screen of tree what I want
i trying use this Sort array values based on parent/child relationship
foreach($xml->children()->children() as $value) {
if($value->active == 1) {
$categories[] = [
'id' => (int) $value->id,
'name' => (string) $value->name->language,
'parent_id' => (int) $value->id_parent
];
}
}
$parent_ids = [];
$parents = ['' => []];
foreach($categories as $val) {
$parents[$val['parent_id']][] = $val;
}
$sorted = $parents[''];
dump($parents); exit;
for($val = reset($sorted); $val; $val = next($sorted)) {
if(isset($parents[$val[0]])) {
foreach($parents[$val[0]] as $next) {
$sorted[] = $next;
}
}
}
The most important thing for me is that everything displays well in select, which is something like this:
-Playstation
-- Playstation 5
--- Gry
--Playstation 3
--- Gry
Anyone can help me?
EDIT:
Problem solved by Build a tree from a flat array in PHP
This should work
usort($arr, function($a, $b) {
return $a["parent_id"] > $b["parent_id"];
});

Update quantity session array

I need a help
This is my session array:
Array
(
[menu] =>
[id] => 3
[products] => Array
(
[0] => Array
(
[id] => 1
[name] => Produkt 1
[code] => 1
[varianta] =>
[pocet] => 1
[price] => 20
[pricepredtym] => 40
)
[1] => Array
(
[id] => 2
[name] => Produkt 1
[code] => 1
[varianta] =>
[pocet] => 1
[price] => 20
[pricepredtym] => 40
)
)
)
I would need about something like, if ($_GET [id] == $ _SESSION ['products'] [id]) and only change this "[pocet]" where [id] = 2
$_GET [id] = 2;
$pocet=5;
[1] => Array
(
[id] => 2
[name] => Produkt 1
[code] => 1
[varianta] =>
[pocet] => 5
[price] => 20
[pricepredtym] => 40
)
You could index your products array by product id. Then updating would be simply:
if(isset($_SESSION['products'][$prod_id])) {
$_SESSION['products'][$prod_id]['pocet'] = $pocet;
}
Otherwise, use a foreach loop:
foreach ($_SESSION['products'] as $i => $prod) {
if ($prod['id'] == $prod_id) {
$_SESSION['products'][$i]['pocet'] = $pocet;
break;
}
}
Compare your GET value with 2 and use it as key of SESSION array.
if ($_GET['id'] == '2'){
$_SESSION['products'][$_GET['id']]['pocet'] = '5';
}

Re-structure array based on parent/child relationship

I have the following array structure:
Array
(
[0] => Array
(
[id] => 83
[parent_id] => 0
[title] => Questionnaire one
)
[1] => Array
(
[id] => 84
[parent_id] => 0
[title] => Questionnaire two
)
[2] => Array
(
[id] => 85
[parent_id] => 83
[title] => Questionnaire three
)
)
I want to re-structure the array so child items are listed under their parents. For example:
Array
(
[0] => Array
(
[id] => 83
[parent_id] => 0
[title] => Questionnaire one
)
[1] => Array
(
[id] => 85
[parent_id] => 83
[title] => Questionnaire three
)
[2] => Array
(
[id] => 84
[parent_id] => 0
[title] => Questionnaire two
)
)
I've searched previous questions but found none of them actually achieve the above.
Can someone please help me with this?
Thanks
You can try
$array = Array(
"0" => Array("id" => 83,"parent_id" => 0,"title" => "Questionnaire one"),
"1" => Array("id" => 84,"parent_id" => 0,"title" => "Questionnaire two"),
"2" => Array("id" => 85,"parent_id" => 83,"title" => "Questionnaire three"));
$id = array_map(function ($item) {return $item["id"];}, $array);
$parent = array_filter($array, function ($item){return $item['parent_id'] == 0;});
$lists = array();
foreach ($parent as $value)
{
$lists[] = $value ;
$children = array_filter($array, function ($item) use($value) {return $item['parent_id'] == $value['id'];});
foreach($children as $kids)
{
$lists[] = $kids ;
}
}
echo "<pre>";
print_r($lists);
Output
Array
(
[0] => Array
(
[id] => 83
[parent_id] => 0
[title] => Questionnaire one
)
[1] => Array
(
[id] => 85
[parent_id] => 83
[title] => Questionnaire three
)
[2] => Array
(
[id] => 84
[parent_id] => 0
[title] => Questionnaire two
)
)
You could use uksort(). Heres a DEMO.
function cmp($a, $b) {
if ($stock[$a] != $stock[$b]) return $stock[$b] - $stock[$a];
return strcmp($a, $b);
}
$a = array(5 => 'apple', 1 => 'banana', 6 => 'orange', 2 => 'kiwi');
uksort($a, "cmp");
foreach ($a as $key => $value) {
echo "$key: $value\n";
}

PHP search array in array

I have array result like this:
Array
(
[0] => stdClass Object
(
[id_global_info] => 78
[name] => rfhd
[body] => dhfdhdf
[contact_author] => mirko
[date_created] => 2012-03-15 16:11:54
[date_expires] => 2012-04-14 16:11:54
[email] =>
[location_id] => 1
[category_id] => 26
[tag] => fhdhfdhfd
[info_type_id] => 4
[user_id] => 3
)
[1] => stdClass Object
(
[id_global_info] => 79
[name] => rfhd
[body] => dhfdhdf
[contact_author] => mirko
[date_created] => 2012-03-15 16:11:56
[date_expires] => 2012-04-14 16:11:56
[email] =>
[location_id] => 1
[category_id] => 26
[tag] => fhdhfdhfd
[info_type_id] => 4
[user_id] => 3
)
[2] => stdClass Object
(
[id_global_info] => 80
[name] => rfhd
[body] => dhfdhdf
[contact_author] => mirko
[date_created] => 2012-03-15 16:11:56
[date_expires] => 2012-04-14 16:11:56
[email] =>
[location_id] => 1
[category_id] => 26
[tag] => fhdhfdhfd
[info_type_id] => 4
[user_id] => 3
)
.
.
.
)
How can I search a multidimensional array and count number of results (for example I want to search for info_type_id with value of 4)?
Use array_filter to filter the array:
function test($arr) {
return $arr["info_type_id"] == 4;
}
echo count(array_filter($yourArray, "test"));
with foreach ?
function searchMyCoolArray($arrays, $key, $search) {
$count = 0;
foreach($arrays as $object) {
if(is_object($object)) {
$object = get_object_vars($object);
}
if(array_key_exists($key, $object) && $object[$key] == $search) $count++;
}
return $count;
}
echo searchMyCoolArray($input, 'info_type_id', 4);
You should try this :
$counter = 0;
$yourArray; // this var is your current array
foreach($yourArray as $object){
if($object->info_type_id == 4){
$counter++;
}
}

Traverse Array and Display In Bullet Points

I want to traverse this array and display, 'comment' as bullet points.
Array
(
[1] => Array
(
[id] => 1
[comment] => a
[parent_id] => 0
[children] => Array
(
[3] => Array
(
[id] => 3
[comment] => c
[parent_id] => 1
[depth] => 0
[child_count] => 0
[children] =>
)
[4] => Array
(
[id] => 4
[comment] => d
[parent_id] => 1
[depth] => 0
[child_count] => 0
[children] =>
)
)
[depth] => 1
[child_count] => 2
)
[2] => Array
(
[id] => 2
[comment] => b
[parent_id] => 0
[children] => Array
(
[5] => Array
(
[id] => 5
[comment] => e
[parent_id] => 2
[children] => Array
(
[7] => Array
(
[id] => 7
[comment] => g
[parent_id] => 5
[children] => Array
(
[8] => Array
(
[id] => 8
[comment] => h
[parent_id] => 7
[children] => Array
(
[9] => Array
(
[id] => 8
[comment] => h
[parent_id] => 8
[children] => Array
(
[10] => Array
(
[id] => 8
[comment] => h
[parent_id] => 9
[depth] => 0
[child_count] => 0
[children] =>
)
)
[depth] => 1
[child_count] => 1
)
)
[depth] => 2
[child_count] => 1
)
)
[depth] => 3
[child_count] => 1
)
)
[depth] => 4
[child_count] => 1
)
[6] => Array
(
[id] => 6
[comment] => f
[parent_id] => 2
[depth] => 0
[child_count] => 0
[children] =>
)
)
[depth] => 5
[child_count] => 2
)
)
You need a little bit of recursion
function traverse_array($array)
{
echo '<ul>';
foreach($array as $element)
{
echo '<li>';
if(isset($element['comment']))
{
echo $element['comment'];
}
if(is_array($element['children']) && count($element['children']) > 0)
{
traverse_array($element['children']);
}
echo '</li>';
}
echo '</ul>';
}
traverse_array($the_big_array);
Here you go, my hierTree() function by default prints nested ul or ol lists, for an undetermined depth of nested arrays, this function will work out of the box for the example array provided in your question.
function hierTree($arr, $tag = 'ul', $key = 'comment', $lvl = 0)
{
$tabs = (!$lvl)? '': str_repeat("\t", $lvl);
reset($arr);
echo "$tabs<$tag>\n";
while (list(, $v) = each($arr))
{
echo "$tabs\t<li>";
echo "{$v[$key]}";
if (count($v['children']))
{
echo "\n";
hierTree($v['children'], $tag, $key, $lvl +1);
echo "$tabs\t";
}
echo "</li>\n";
}
echo "$tabs</$tag>\n";
}
hierTree($tree);
The output of this function will be nicely indented for it to be easily readable.
Also, If you do hierTree($tree, 'ol'); you will get an ordered list. Id you do hierTree($tree, 'ol', 'id'); You will get an ordered tree and the id field will be print instead of the default comment one.
Inserting classes.
If you want to have different classes per list element so you can more easily style on CSS. (although I would recomment to use CSS direct descendant selectors (“>”))
function hierTree($arr, $tag = 'ul', $key = 'comment', $lvl = 0)
{
$tabs = (!$lvl)? '': str_repeat("\t", $lvl);
reset($arr);
echo "$tabs<$tag class=\"depth$lvl\">\n"; // ← The change is there.
while (list(, $v) = each($arr))
{
echo "$tabs\t<li>";
echo "{$v[$key]}";
if (count($v['children']))
{
echo "\n";
hierTree($v['children'], $tag, $key, $lvl +1);
echo "$tabs\t";
}
echo "</li>\n";
}
echo "$tabs</$tag>\n";
}
This slightly modified version will print a depthN class per list element.
So you can then target their *LI*s by a simple CSS rule such as ul.depth1 > li { ....

Categories