I need to access multiple arrays, the problem lies when I get to the arrays I need like below, I can't access it traditionally because the key will be different every time.
I'm dealing with the following array:
Array
(
[oe_schedule_charge] => Array
(
[617cdb2797153d6fbb03536d429a525b] => Array
(
[schedule] =>
[args] => Array
(
[0] => Array
(
[id] => cus_2OPctP95LW8smv
[amount] => 12
)
)
)
)
)
There are going to be hundreds of these arrays and I need a way to efficiently access the data within them. I'm using the following code with expected output:
function printValuesByKey($array, $key) {
if (!is_array($array)) return;
if (isset($array[$key]))
echo $key .': '. $array[$key] .'<br>';
else
foreach ($array as $v)
printValuesByKey($v, $key);
}
$cron = _get_cron_array();
foreach( $cron as $time => $hook ) {
if (array_key_exists('oe_schedule_charge', $hook)) {
echo '<div>';
echo date('D F d Y', $time);
echo printValuesByKey($hook, 'amount');
echo printValuesByKey($hook, 'id');
echo '</div>';
}
}
But I've never had to deal with this much data, so I would like to take the proper precautions. Any light that can be shed on accessing a multidimensional array like this in an efficient way would be greatly appreciated.
I would consider loading it into an object, then writing member functions to get what you want.
class myclass {
private $_uniqueKey;
private $_schedule;
private $_args = array();
private $_amount = array();
private $_id = array();
public function __construct($arrayThing)
{
foreach($arrayThing['oe_schedule_charge'] as $uniqueKey => $dataArray)
{
$this->_uniqueKey = $uniqueKey;
$this->_schedule = $dataArray['schedule'];
$this->_args = $dataArray['args'];
}
$this->_afterConstruct();
}
private function _afterConstruct()
{
foreach($this->_args as $argItem)
{
if(isset($argItem['amount']) && isset($argItem['id']))
{
$this->_amount[] = $argItem['amount'];
$this->_id[] = $argItem['id'];
}
}
}
public function getUniqueKey()
{
return $this->_uniqueKey;
}
public function getSchedule()
{
return $this->_schedule;
}
public function getArgs()
{
return $this->_args;
}
public function printShitOut($time)
{
//You define this. But if you do a print_r( on the object, it will tell you all the items you need. )
}
//code would be like this:
$cron = _get_cron_array();
foreach( $cron as $time => $hook )
{
$obj = new myclass($hook);
$obj->printShitOut($time);
}
Related
I created a cache from xml, and by a construct I generate the object which finally become the arrays. And everything would be ok, if the key of these arrays wasnt "0". I dont know how it works. I searched the information how to change the class, or how to replace the keys. I am stuck. Could you help me with this.
$xml = simplexml_load_file($cache);
}
class Property {
public $xmlClass;
public $elemClass = '';
public $result_array = [];
public $data = '';
public function __construct($xml,$elem) {
$this->xmlClass=$xml;
$this->elemClass=$elem;
foreach($xml->list->movie as $value) {
$data = $value->$elem;
$this->result_array[] = $data;
}
}
public function getResult() {
return $this->result_array;
}
}
$result_zn = new Property($xml,'zn');
$result_au = new Property($xml,'au');
$result_ti = new Property($xml, 'ti');
$zn = $result_zn->getResult();
$au = $result_au->getResult();
$ti = $result_ti->getResult();
I think you can use the function array_values() to get the key 0,like this:
$arr = array(
'1' => 'cat',
'2' => 'dog'
);
$newarr = array_values($arr);
print_r($newarr);
and the result is :
Array ( [0] => cat [1] => dog )
I am trying to develop my understanding of php and would really appreciate any help. I have used a while loop to compare some values posted in my form with what is stored in a csv file.
This code works well. However, is it possible to achieve the same result using a FOR EACH loop or even a Do Until?? Or both?? Many thanks for any support
$file_handle = fopen("games.csv", "r"); # identify which file is being used.
while(!feof($file_handle))
{
$gameinfo = fgetcsv($file_handle);
if ($gameinfo[0] == $_POST["gameID"])
{
$GameName = "$gameinfo[2]";
$GameCost = "$gameinfo[4]";
$GameFound = true;
}
}
fclose($file_handle);
while is the best suited statement for this task, because you want to check for EOF before doing any read.
You can transform it in a do-while (there is no do-until in PHP) as an exercise:
do
{
if (!feof($file_handle))
{
$gameinfo = fgetcsv($file_handle);
if ($gameinfo[0] == $_POST["gameID"])
{
...
}
}
}
while(!feof($file_handle));
or shorter
do
{
if (feof($file_handle))
break;
$gameinfo = fgetcsv($file_handle);
if ($gameinfo[0] == $_POST["gameID"])
{
...
}
}
while(true);
but that's just a bad way to write a while.
Regarding foreach, quoting the doc
The foreach construct provides an easy way to iterate over arrays. foreach works only on arrays and objects, and will issue an error when you try to use it on a variable with a different data type or an uninitialized variable.
You can customize iteration over objects, this let you (Warning, layman language) use foreach on "custom objects" so that you can, in a way, extend the functionality of foreach.
For example to iterate over CSV files you can use this class
<?php
class FileIterator implements Iterator
{
private $file_handle = null;
private $file_name;
private $line;
public function __construct($file_name)
{
$this->file_name = $file_name;
$this->rewind();
}
public function rewind()
{
if (!is_null($this->file_handle))
fclose($this->file_handle);
$this->line = 1;
$this->file_handle = fopen($this->file_name, "r");
}
public function current()
{
return fgetcsv($this->file_handle);
}
public function key()
{
return $this->line;
}
public function next()
{
return $this->line++;
}
public function valid()
{
$valid = !feof($this->file_handle);
if (!$valid)
fclose($this->file_handle);
return $valid;
}
}
?>
and use it this way
$it = new FileIterator("game.csv");
foreach ($it as $line => $gameObj)
{
echo "$line: " . print_r($gameObj, true) . "<br/>";
}
Which produce something like
1: Array ( [0] => 0 [1] => Far Cry 3 )
2: Array ( [0] => 1 [1] => Far Cry Primal )
3: Array ( [0] => 2 [1] => Alien Isolation )
for this file
0,Far Cry 3
1,Far Cry Primal
2,Alien Isolation
I have no idea why my query is suddenly not working (although maybe it was never working to begin with). Is there anything wrong here?
My controller;
$dataset = $postcode_lookup->dataset; // will return "201502_postcode"
$postcode_extract = new PostcodeExtract;
$postcode_extract = $postcode_extract->setTableByDate($dataset);
foreach ($input as $column => $values) {
$postcode_extract->orWhere(function ($query) use ($column, $values) {
$query->whereIn($column, $values);
});
}
/*
* Temporarily print out the raw SQL...
*/
$sql = str_replace(['%', '?'], ['%%', "'%s'"], $postcode_extract->toSql());
$fullSql = vsprintf($sql, $postcode_extract->getBindings());
print_r($fullSql);
exit;
My model;
<?php namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class PostcodeExtract extends Model {
protected $connection = 'postcodes';
public function setTableByDate($selected_tablename)
{
$this->table = $selected_tablename;
// Return $this for method chaining
return $this;
}
public function getTable()
{
if (isset($this->table))
$this->setTableByDate($this->table);
return $this->table;
}
All that the print out of the raw sql is returning is select * from 201502_postcode and is just ignoring the rest of the query.
This is my $input array;
Array
(
[country] => Array
(
[0] => L93000001
[1] => M83000003
)
[county] => Array
(
[0] => 95
)
)
orWhere and whereIn is just being ignored it seems..
--- UPDATE ---
I didn't know about orWhereIn. But having tried this;
foreach ($input as $column => $values) {
$postcode_extract->orWhereIn($column, $values);
print "<br>";
print $column;
print "<br>";
print_r($values);
print "<br>";
}
I still get the same result. It's like this loop is being totally ignored - even though I can get these print's/print_r's to work. The raw SQL is still just select * from 201502_postcode.
Try this
$postcode_extract->orWhereIn($column, $values)
Also, if that doesnt work, try making the first one condition a whereIn and the rest a orWhereIn
Oh this is so frustratingly obvious I think I might cry...
foreach ($input as $column => $values) {
$postcode_extract = $postcode_extract->orWhereIn($column, $values);
}
I am developing a web application in PHP,
I need to transfer many objects from server as JSON string, is there any library existing for PHP to convert object to JSON and JSON String to Objec, like Gson library for Java.
This should do the trick!
// convert object => json
$json = json_encode($myObject);
// convert json => object
$obj = json_decode($json);
Here's an example
$foo = new StdClass();
$foo->hello = "world";
$foo->bar = "baz";
$json = json_encode($foo);
echo $json;
//=> {"hello":"world","bar":"baz"}
print_r(json_decode($json));
// stdClass Object
// (
// [hello] => world
// [bar] => baz
// )
If you want the output as an Array instead of an Object, pass true to json_decode
print_r(json_decode($json, true));
// Array
// (
// [hello] => world
// [bar] => baz
// )
More about json_encode()
See also: json_decode()
for more extendability for large scale apps use oop style with encapsulated fields.
Simple way :-
class Fruit implements JsonSerializable {
private $type = 'Apple', $lastEaten = null;
public function __construct() {
$this->lastEaten = new DateTime();
}
public function jsonSerialize() {
return [
'category' => $this->type,
'EatenTime' => $this->lastEaten->format(DateTime::ISO8601)
];
}
}
echo json_encode(new Fruit()); //which outputs:
{"category":"Apple","EatenTime":"2013-01-31T11:17:07-0500"}
Real Gson on PHP :-
http://jmsyst.com/libs/serializer
http://symfony.com/doc/current/components/serializer.html
http://framework.zend.com/manual/current/en/modules/zend.serializer.html
http://fractal.thephpleague.com/ - serialize only
json_decode($json, true);
// the second param being true will return associative array. This one is easy.
PHP8-Code:
class foo{
function __construct(
public $bar,
protected $bat,
private $baz,
){}
function getBar(){return $this->bar;}
function getBat(){return $this->bat;}
function getBaz(){return $this->baz;}
}
//Create Object
$foo = new foo(bar:"bar", bat:"bat", baz:"baz");
//Object => JSON
$fooJSON = json_encode(serialize($foo));
print_r($fooJSON);
// "O:3:\"foo\":3:{s:3:\"bar\";s:3:\"bar\";s:6:\"\u0000*\u0000bat\";s:3:\"bat\";s:8:\"\u0000foo\u0000baz\";s:3:\"baz\";}"
// Important. In order to be able to unserialize() an object, the class of that object needs to be defined.
# More information here: https://www.php.net/manual/en/language.oop5.serialization.php
//JSON => Object
$fooObject = unserialize(json_decode($fooJSON));
print_r($fooObject);
//(
# [bar] => bar
# [bat:protected] => bat
# [baz:foo:private] => baz
# )
//To use some functions or Properties of $fooObject
echo $fooObject->bar;
// bar
echo $fooObject->getBat();
// bat
echo $fooObject->getBaz();
// baz
I made a method to solve this.
My approach is:
1 - Create a abstract class that have a method to convert Objects to Array (including private attr) using Regex.
2 - Convert the returned array to json.
I use this Abstract class as parent of all my domain classes
Class code:
namespace Project\core;
abstract class AbstractEntity {
public function getAvoidedFields() {
return array ();
}
public function toArray() {
$temp = ( array ) $this;
$array = array ();
foreach ( $temp as $k => $v ) {
$k = preg_match ( '/^\x00(?:.*?)\x00(.+)/', $k, $matches ) ? $matches [1] : $k;
if (in_array ( $k, $this->getAvoidedFields () )) {
$array [$k] = "";
} else {
// if it is an object recursive call
if (is_object ( $v ) && $v instanceof AbstractEntity) {
$array [$k] = $v->toArray();
}
// if its an array pass por each item
if (is_array ( $v )) {
foreach ( $v as $key => $value ) {
if (is_object ( $value ) && $value instanceof AbstractEntity) {
$arrayReturn [$key] = $value->toArray();
} else {
$arrayReturn [$key] = $value;
}
}
$array [$k] = $arrayReturn;
}
// if it is not a array and a object return it
if (! is_object ( $v ) && !is_array ( $v )) {
$array [$k] = $v;
}
}
}
return $array;
}
}
Background
Assume I have the following nested variable in PHP.
$data = Array(
Array('lname' => 'Simpson','fname' => 'Homer','age' => '35','motto' => '_blank_'),
Array('lname' => 'Simpson','fname' => 'Marge','age' => '34','motto' => '_blank_'),
Array('lname' => 'Flintstone','fname' => 'Fred','age' => '33','motto' => '_blank_'),
Array('lname' => 'Flintstone','fname' => 'Wilma','age' => '29','motto' => '_blank_')
);
Assume also the standard methods for accessing specific values:
print($data[0]['fname']); // Homer
print($data[1]['age']); // 34
Question
Is there an existing library or framework that would allow me to easily
acess specific values declaratively, without using foreach loops?
$test = $data->get_record_by_fname['Homer']
print $test['age'] //35
If you really wanted to overkill everything, you could try an approach using magical methods!
class Simpsons
{
protected $_data = array();
public function __construct(array $data)
{
$this->_data = array_map(function ($i) { return (object)$i; }, $data);
}
public function __call($method, $args)
{
if (count($args) == 0)
return NULL;
foreach ($this->_data as $row)
{
if (property_exists($row, $method) && $row->$method == $args[0])
{
return $row;
}
}
return NULL;
}
}
Usage:
$p = new Simpsons($data); // Stored in the format provided
var_dump($p->fname('Homer')); // Gets the record with fname = Homer
Is there a particular reason you don't want to use foreach loops? If it's merely for conciseness, you could just declare the function yourself, it's fairly trivial:
function get_record($set, $field, $value) {
foreach($set as $key => $val) {
if($val[$field] === $value) return $set[$key];
}
return NULL;
}
Then your example would become:
$test = get_record($data, 'fname', 'Homer');
print $test['age']; //35
class SomeClass{
// Stores the Array of Data
public $data;
// Sets up the object. Only accepts arrays
public function __construct(array $data)
{
$this->data = $data;
}
// Gets a record based on the key/value pair
public function getByKey($key, $value)
{
foreach($this->data as $array)
{
if(is_array($array)
{
if(array_key_exists($key, $array) && $array[$key] == $value)
{
return $array;
}
}
}
}
}
$array = array( 1 => array("Test" => "Hello"));
$obj = new SomeClass($array);
$record = $obj->getByKey('Test', 'Hello');
This lets you get a record based on what a key/value pair inside the array is. Note, the type hinting in the constructor is PHP 5.3(?)
BTW, No, there is no way to escape the foreach as even internal PHP functions (anything beginning with array_) uses a foreach or some other type of loop. However, if you encapsulate the loop into a class, you don't have to think about it.