Monday, November 23, 2015

how to make a array path

first thank for all CakePHP. I learned a lot of thing when i work with your framework and I hope I can be useful one day

I am writing this little code to cycle through the array and I think that it can be useful

<?php

function set($path,$value,&$point){
    $aPath
= explode('.',$path);
   
if(count($aPath)>1){
        $path
= strstr($path,'.');
        $path
= substr($path,-strlen($path)+1);
       
if( !isset($point[$aPath[0]]) || !is_array($point[$aPath[0]])){
            $point
[$aPath[0]] = [];
       
}
       
return set($path,$value,$point[$aPath[0]]);
   
}else{
        $point
[$aPath[0]] = $value;
   
}
   
return true;
}

//test
$myArray
= [];
set('foo.bar','it works',$myArray);
set('foo.foo',['bar' =>'it works'],$myArray);
set('a.b.c.d.e.f','it works',$myArray);
var_dump
($myArray);



--
Like Us on FaceBook https://www.facebook.com/CakePHP
Find us on Twitter http://twitter.com/CakePHP

---
You received this message because you are subscribed to the Google Groups "CakePHP" group.
To unsubscribe from this group and stop receiving emails from it, send an email to cake-php+unsubscribe@googlegroups.com.
To post to this group, send email to cake-php@googlegroups.com.
Visit this group at http://groups.google.com/group/cake-php.
For more options, visit https://groups.google.com/d/optout.

No comments: