I've seen a few example here on stack but they don't seem to cover this scenario.
I'm attempting this;
$flight_range = array(
array('range' => range(1,50), 'service' => 'Long Haul'),
array('range' => range(51,54), 'service' => 'Short Haul'),
....
);
but PHP won't let me. It returns;
Parse error: syntax error, unexpected '(', expecting ')' on line 02
This does not work either;
array(range(1,50), range(51,54) ...
The problem is with trying to assign a value of range().
I have 20+ sets of range values to assign.
Can anyone suggest an easy method for achieving these sorts of array values?
EDIT;
haike00, Jack and Sean are right.
Maybe my question should be how do i make $flight_range a member variable of a class;
private $flight_range = array(array('range' => range(1,50), 'service' => 'Long Haul'));
What is the problem with doing this in your constructor?
class MyClass {
private $flight_range;
public function __construct() {
$this->flight_range = array(
array(
'range' => range( 1, 50 ),
'service' => 'Long Haul'
)
);
}
}
Its working fine on my end.
$flight_range = array(
array('range' => range(1,50) , 'service' => 'Long Haul'), array('range' => range(51,54),'service' => 'Short Haul'));
print_r($flight_range);
Just copy and paste above code and run it.
Related
Sorry not the best title.
I am building an application with many different classes that will be called by a document builder class that takes an Array of what are defined as segments that are then used to reference the segments class to then be built into the document. I provided the Document class and the SegmentConfig file that references which segment points to which file location.
Reference Below First
I could not find a definitive answer on how I could use this to then generate the segment object
could I do it like use $segmentObjectLocation as it would be equal to something like 'Edi\Segments\AmtSegment'. If anyone has some insight.
Other info: the reason I am trying to do it some way like this is becuase this will be used in multiple places though out the application.
$segmentLocation = include('SegmentConfig.php');
class Document{
public function __construct($structure){
$this -> documentStructure = $structure;
foreach($structure as $segment){
buildSegment($segment);
}
}
public function buildSegment($segment){
$segmentObjectLocation = $segmentLocation ->{$segment};
}
}
<?php
namespace Edi;
SegmentConfig.php
return (object) array(
'AMT' => 'Edi\Segments\AmtSegment',
'B4' => 'Edi\Segments\B4Segment',
'BEG' => 'Edi\Segments\BegSegment',
'CTT' => 'Edi\Segments\CttSegment',
'DTM' => 'Edi\Segments\DtmSegment',
'FOB' => 'Edi\Segments\FobSegment',
'GE' => 'Edi\Segments\GeSegment',
'GS' => 'Edi\Segments\GsSegment',
'IEA' => 'Edi\Segments\IeaSegment',
'ISA' => 'Edi\Segments\IsaSegment',
'MSG' => 'Edi\Segments\MsgSegment',
'N1' => 'Edi\Segments\N1Segment',
'N2' => 'Edi\Segments\N2Segment',
'N3' => 'Edi\Segments\N3Segment',
'N4' => 'Edi\Segments\N4Segment',
'N9' => 'Edi\Segments\N9Segment',
'PER' => 'Edi\Segments\PerSegment',
'PID' => 'Edi\Segments\PidSegment',
'PO1' => 'Edi\Segments\Po1Segment',
'Q2' => 'Edi\Segments\Q2Segment',
'R4' => 'Edi\Segments\R4Segment',
'REF' => 'Edi\Segments\RefSegment',
'SAC' => 'Edi\Segments\SacSegment',
'SE' => 'Edi\Segments\SeSegment',
'ST' => 'Edi\Segments\StSegment',
'TC2' => 'Edi\Segments\Tc2Segment',
'TD1' => 'Edi\Segments\Td1Segment',
'TD4' => 'Edi\Segments\Td4Segment',
'TD5' => 'Edi\Segments\Td5Segment',
)
Not that this sounds like a particularly great idea, but:
class Foo {}
$n = 'Foo';
$f = new $n();
var_dump($f);
Output:
object(Foo)#1 (0) {
}
I'm trying to pass data about the page state (navbar links having active class when you are in that exact page), page title. I do so with an indexed array $pageInfo, however I am getting a syntax error and doen't know where?
Also do you think this is a good method or should I use view->share() instead?
public function clases()
{
$pageInfo[] =
(
'page_title' => 'Clases',
'menu_active' => 'CLases',
'sub_menu_active' => '',
);
return view('clases.list', compact('pageInfo'));
}
public function domicilio()
{
$pageInfo[] =
(
'page_title' => 'Clases a domicilio',
'menu_active' => 'Clases',
'sub_menu_active' => 'Clases a domicilio',
);
return view('clases.domicilio', compact('pageInfo'));
I suggest you read PHP basic syntax.
Basically you want to do this:
$pageInfo =
[
'page_title' => 'Clases',
'menu_active' => 'CLases',
'sub_menu_active' => '',
];
Arrays have a syntax of [key => val, ...] in PHP, you're using () as it seems.
Also $someArray[] = someValue, will append the someValue to an existing array, in your case that would create another, unwanted level of your array.
And last, you're not ending the domicilio() function. But I'll assume you just didn't paste it in (you should add } at the end, if that's not the case).
This question already has answers here:
Is it possible to define a class property value dynamically in PHP?
(2 answers)
Closed 7 years ago.
PHP version 5.6. Code:
protected $siteServices = [
1 => [
'title' => 'Consulting',
'key' => 'service',
'description' => '',
'file' => asset('assets/img/sample/image1.jpg'), // throws error on this line
],
];
Error: PHP Parse error: syntax error, unexpected '(', expecting ']'
What would be a possible fix for this?
Edit
Solved by moving the variable in the running function instead of making it protected. Also can be solved by declaring the empty variable first then set the values in __constructor()
It could be done but it in a different way. I have listed two different methods.
First method
protected $siteServices = [
1 => [
'title' => 'Consulting',
'key' => 'service',
'description' => '',
'file' => ['asset', 'assets/img/sample/image1.jpg'] // throws error on this line
]
];
I replaced the function call with an array assigned to the file key in this array. So, now, you'd be wondering how you could call the asset function, yeah? It is pretty simple.
While you are looping through that array, you can do this:
call_user_func($siteServices[1]['file'][0], $siteServices[1]['file'][1]);
So, what are we doing here?
Firstly, we set an array to the file key where the first element of the array is the name of the function, while the other element is the value for the parameter that needs to be passed to the function defined previously.
So, using the PHP's call_user_func function, you can call functions giving their name and the parameters. I hope this helps you out.
Second method
I am pretty sure you will have a setter function for this property. Therefore, you could do something like this:
public function setSiteService($title, $key, $description, $file)
{
$file = asset($file);
$service = [
'title' => $title,
'key' => $key,
'description' => $description,
'file' => $file
];
$this->siteServices[] = $service;
}
So, the setter does the processing part for you. Hardcoding arrays is not at all a good idea, you should definitely populate through some mechanism.
I created this array of objects:
$ad_1 = new AdUnit(array('id' => '1', 'name' => 'Ad_1', 'description' => 'great ad', 'code' => 'alpha', 'widget_id' => '123'));
$ad_2 = new AdUnit(array('id' => '2', 'name' => 'Ad_2', 'description' => 'good ad', 'code' => 'delta', 'widget_id' => '456'));
$ad_3 = new AdUnit(array('id' => '3', 'name' => 'Ad_3', 'description' => 'bad ad', 'code' => 'sigma', 'widget_id' => '789'));
$adUnitArr = array($ad_1, $ad_2, $ad_3);
and i want to check that a random ad i got from a function exists in the array. the code to get the ad looks like this:
$fixture = new AdGroup();
$fixture->setAds($adUnitArr);
$randad = $fixture->getRandomAd();
now i want to check if the array contains the random ad i received, what i was able to do like this:
$this->assertEquals(in_array($randad, $adUnitArr), 1); //check if ad in array
but my question is, is there an assert or some other way to check this thing better than the way i did it?? i tried using assertArrayHasKey but i got the following error:
PHPUnit_Framework_Exception: Argument #1 (No Value) of PHPUnit_Framework_Assert::assertArrayHasKey() must be a integer or string
any idea please? thx
Try the assertContains method:
$this->assertContains( $randad, $adUnitArr );
i want to place an class function as array value, but it shows parsing error:
Linter error message - Expecting `']"
Php error message -
ErrorException [ Parsing Error ]: syntax error, unexpected '['
Example
'name' => [
'data_type' => 'varchar',
'label' => Lang::get('site.general.name'),
...
As you see, i want the label to be the value returned from the "lang" class "get" function.
Array short syntax [i.e. $a = ['a', 'b']] is available from version 5.4 on.
Be sure to have the right PHP version to use it, otherwise you should stick to old array('a', 'b') syntax.
You have a syntax error on key name line.
Try syntax like this:
$array = array(
'name' => 'val',
'data_type' => 'varchar',
'label' => Lang::get('site.general.name'),
'array' => array(
...
)
);
And be sure that Lang::get('site.general.name') return a value.
Change [ to array( like this:
'name' => array(
'data_type' => 'varchar',
'label' => Lang::get('site.general.name'),
[] for defining arrays isn't supported in older versions.