Asked  7 Months ago    Answers:  5   Viewed   29 times

I have a hierarchical array in my project like this:

$Array = array(
    array(
        'Id' => 1,
        'Title' => 'Some Text1',
        'Children' => array(
            array(
                'Id' => 11,
                'Title' => 'Some Text11',
                'Children' => array(
                    array(
                        'Id' => 111,
                        'Title' => 'Some Text111',
                    ),
                    array(
                        'Id' => 112,
                        'Title' => 'Some Text112',
                        'Children' => array(
                            array(
                                'Id' => 1121,
                                'Title' => 'Some Text1121',
                            )
                        )
                    )
                )
            ),
            array(
                'Id' => 12,
                'Title' => 'Some Text12',
                'Children' => array(
                    array(
                        'Id' => 121,
                        'Title' => 'Some Text121',
                    )
                )
            )
        )
    ),
    array(
        'Id' => 2,
        'Title' => 'Some Text2',
    )
);

I want to search my string (such as 'Some Text1121') in 'Title' index in this array and return it's path such as after search 'Some Text1121' I want to return this result:

"1 -> 11 -> 112 -> 1121"

Or when I Search 'Some' string, return all path in array. please help me, thanks.

 Answers

52

I've quickly written you something. It's not perfect, but you get the idea:

<?php

function searchRec($haystack, $needle, $pathId = Array(), $pathIndex = Array()) {
    foreach($haystack as $index => $item) {
        // add the current path to pathId-array
        $pathId[] = $item['Id'];
        // add the current index to pathIndex-array
        $pathIndex[] = $index;
        // check if we have a match
        if($item['Title'] == $needle) {
            // return the match
            $returnObject = new stdClass();
            // the current item where we have the match
            $returnObject->match = $item;   
            // path of Id's (1, 11, 112, 1121)
            $returnObject->pathId = $pathId; 
            // path of indexes (0,0,1,..) - you might need this to access the item directly
            $returnObject->pathIndex = $pathIndex; 
            return $returnObject;
        }

        if(isset($item['Children']) && count($item['Children']>0)) {
            // if this item has children, we call the same function (recursively) 
            // again to search inside those children:
            $result = searchRec($item['Children'], $needle, $pathId, $pathIndex);
            if($result) {
                // if that search was successful, return the match-object
                return $result;
            }
        }
    }
    return false;
}

// useage:
$result = searchRec($Array, "Some Text11");
var_dump($result);

// use 
echo implode(" -> ", $result->pathId);
// to get your desired 1 -> 11 -> 112

EDIT: rewritten to make the function actually return something. It now returns an Object with the matching item, the path of Id's and the path of (array-) Indexes.

Wednesday, March 31, 2021
 
juanrpozo
answered 7 Months ago
92

In theory this should work:

function findChild(&$array){
     foreach($array as &$arr){
            if(isset($arr['children'])){
                  $arr['leaf'] = 0; //there are children
                  findChild($arr['children']);
            }
            else {
                  $arr['leaf'] = 1; //there are no children
            }
     }
}

Here is a working demo: http://codepad.org/AnYiRpES

Wednesday, March 31, 2021
 
rasmusx
answered 7 Months ago
39

When you set cell values individually, you have the option of setting the datatype explicitly, but when you use the fromArray() method, you don't have this option.

However, by default, PHP uses a default value binder to identify datatypes from the values passed, and set the cell datatype accordingly. This default behaviour is defined in a class /PHPExcel/Cell/DefaultValueBinder.php.

So you can create your own value binder, as described in the PHPExcel Documentation, that would set every value as a string datatype.

Something like:

class PHPExcel_Cell_MyColumnValueBinder extends PHPExcel_Cell_DefaultValueBinder implements PHPExcel_Cell_IValueBinder
{
    protected $stringColumns = [];

    public function __construct(array $stringColumnList = []) {
        // Accept a list of columns that will always be set as strings
        $this->stringColumns = $stringColumnList;
    }

    public function bindValue(PHPExcel_Cell $cell, $value = null)
    {
        // If the cell is one of our columns to set as a string...
        if (in_array($cell->getColumn(), $this->stringColumns)) {
            // ... then we cast it to a string and explicitly set it as a string
            $cell->setValueExplicit((string) $value, PHPExcel_Cell_DataType::TYPE_STRING);
            return true;
        }
        // Otherwise, use the default behaviour
        return parent::bindValue($cell, $value);
    }
}

// Instantiate our custom binder, with a list of columns, and tell PHPExcel to use it
PHPExcel_Cell::setValueBinder(new PHPExcel_Cell_MyColumnValueBinder(['A', 'B', 'C', 'E', 'F']));

$objPHPExcel = new PHPExcel();
$objPHPExcel->getActiveSheet()->fromArray($dataArray,null,"A2");
Friday, May 28, 2021
 
Wilk
answered 5 Months ago
59

Can never avoid the loop :-)

function search($array, $searchString){
   foreach($array as $key=>$val){
        if(in_array($searchString, $val)) return true;
   }
   return false;
}

//use it like so:
if(search($array, '109.148.183.1')){/*do something*/}
Saturday, May 29, 2021
 
DaveRandom
answered 5 Months ago
13

There's a lot been written about PHPExcel and memory use, and I'm not going to repeat it all here.

Try reading some of the threads on the PHPExcel discussion board discussing the issue, such as this one; or previous answers here on SO such as this one or this one

Tuesday, July 6, 2021
 
Slinky
answered 4 Months ago
Only authorized users can answer the question. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :